2017-04-23 13 views
0

私はboostを使って2つのstd :: u16stringインスタンスで大文字と小文字を区別しない文字列コンパートメントを実行しようとしています。私の検索に基づいて、私が行っているロケールを生成する必要があります。boost :: iequalsをstd :: u16stringで使用する

#include <boost/algorithm/string.hpp> 
#include <boost/locale.hpp> 

#include <locale> 
#include <iostream> 

int main() { 
    // Create the strings 
    std::u16string str1 = boost::locale::conv::utf_to_utf<char16_t>("unicode"); 
    std::u16string str2 = boost::locale::conv::utf_to_utf<char16_t>("UNICODE"); 

    // Create the locale 
    boost::locale::generator gen; 
    std::locale loc = gen(""); 

    // Doesn't matter if I do this or not 
    //std::locale::global(loc); 

    // Try to compare 
    if (boost::iequals(str1, str2, loc)) { 
     std::cout << "EQUAL\n"; 
    } else { 
     std::cout << "!EQUAL\n"; 
    } 

    return 0; 
} 

これは、STDになり:: bad_cast例外:私は間違って何をやっている

terminate called after throwing an instance of 'std::bad_cast' 
    what(): std::bad_cast 

を?

答えて

1

std::u16stringは、ご存知のようにchar16_tです。

boost::iequalsは、内部でstd::toupperを使用して2つの文字列を比較します。

std::toupperは、std::ctype<cT>のファセットサポートが必要です。ここではct = char16_tです。このanswerで説明されているように、このサポートは標準では要求されていないため、ほとんどの実装ではサポートされていません。

ファセットstd :: ctypeは、文字タイプの拡大、縮小、および分類をサポートするために、特殊化して使用済みファセットに配置する必要があります。 char16_tまたはchar32_tの準備ができていません。

あなたは何も間違っていないので、サポートはそこにはありません。 16ビットのUnicode文字列のサポートが本当に必要な場合は、Qtのようなサードパーティのライブラリをお勧めします。クラスQStringは16ビットの文字by defaultを使用しています。

関連する問題