コンマが小数点記号であるドイツのロケールで、boost::locale と std::stod を一緒に使用しようとしています。次のコードを検討してください。
boost::locale::generator gen;
std::locale loc(""); // (1)
//std::locale loc = gen(""); // (2)
std::locale::global(loc);
std::cout.imbue(loc);
std::string s = "1,1"; //float string in german locale!
double d1 = std::stod(s);
std::cout << "d1: " << d1 << std::endl;
double d2 = 2.2;
std::cout << "d2: " << d2 << std::endl;
std::locale loc("") は正しいロケールを作成し、出力は
d1: 1,1
d2: 2,2
私が期待するように。行(1)をコメントアウトし、行(2)のコメントを外すと、出力は次のようになります
d1: 1
d2: 2.2
d2 の結果は予想どおりです。私が理解している限り、boost::locale では、d2 を数値としてフォーマットし、実行する必要があることを明示的に指定する必要があります。
std::cout << "d2: " << boost::locale::as::number << d2 << std::endl;
出力を 2,2 に再度固定します。問題は、std::stod が 1,1 を有効な浮動小数点数と見なさず、1 に切り捨てることです。
私の質問は、boost::locale でロケールを生成すると std::stod が機能しなくなるのはなぜですか?
追加情報: VC++2015、Boost 1.60、ICU なし、Windows 10 を使用しています
アップデート:
最初に std::locale("") を使用し、次に boost を使用して、グローバル ロケールを 2 回設定すると、問題が修正されることに気付きました。
std::locale::global(std::locale(""));
bl::generator gen;
std::locale::global(gen(""));
しかし、なぜこのように振る舞うかはわかりません!