skip to Main Content

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 *>’

enter image description here
enter image description here

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


  1. It does work for me with gen("") as well as gen("en_US.utf8"):

    enter image description here

    If it doesn’t work for you, check that

    • Your source file encoding (UTF8 is assumed here)
    • Boost is built with ICU support
    • your system supports UNICODE locales

    Login or Signup to reply.
  2. Problem is most probably handling encoding.

    If you are using MSVC make sure, that:

    • source file is encoded with UTF-8
    • compler received /utf-8 switch – to properly read source code and generate executable which for strings uses UTF-8
    • locale is configured properly:
    #include <iostream>
    #include <locale>
    #include <string_view>
    
    #include <boost/locale.hpp>
    
    using namespace std::string_view_literals;
    
    int main()
    {
        std::locale::global(std::locale(".utf-8")); // inform standard library that executable is using UTF-8 encoding
        std::cout.imbue(std::locale("")); // use system locale on standard output
    
        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(".utf-8")};
        for (const auto& i : map)
        {
            std::cout << i << 'n';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search