How to do this in C++ using boost locale?
In one of the questions I found an example
Cross-platform iteration of Unicode string (counting Graphemes using ICU)
#include <iostream>
#include <string_view>
#include <boost/locale.hpp>
using namespace std::string_view_literals;
int main()
{
boost::locale::generator gen;
auto string = "noël 😸😾"sv;
boost::locale::boundary::csegment_index map{
boost::locale::boundary::character, std::begin(string),
std::end(string), gen("")};
for (const auto& i : map)
{
std::cout << i << 'n';
}
}
This code turned out to be non-working.How can I fix it?
Error:
E0289 no instance of constructor "boost::locale::boundary::segment_index::segment_index [with BaseIterator=const char *]" matches the argument list
C2440 ‘initializing’: cannot convert from ‘initializer list’ to ‘boost::locale::boundary::segment_index<const char *>’
Version of boost: 1.81.0, I use a pre-release version of the C++23 and C17 standard. Visual Studio. The boost is statically bonded. Icu is installed. File encoded utf8. I compile the project as release x64
2
Answers
It does work for me with
gen("")
as well asgen("en_US.utf8")
:If it doesn’t work for you, check that
Problem is most probably handling encoding.
If you are using MSVC make sure, that:
/utf-8
switch – to properly read source code and generate executable which for strings uses UTF-8