std::string 的 operator< 是否应该受当前语言环境的影响?

Should operator< for std::string be affected by current locale?

我对这个事实有点惊讶

#include <string>
#include <cassert>
#include <cstdio>

int main()
{
    printf("Floating point format: %.7g\n",1.4);
    std::string a("Hello, World/#");
    std::string b("Hello, World 2");

    assert(a>b); 

    assert(setlocale(LC_ALL,"sv_SE.UTF-8")!=NULL);
    printf("New floating point format: %.7g\n",1.4);

    assert(b<a);
}

http://coliru.stacked-crooked.com/a/1b435636e4ff7161

本程序正常退出。结论,语言环境会影响 a 和 b 之间的比较。那是对的吗?这意味着当在 std::set 等中使用 std::string 作为键时,排序不变量会因当前语言环境的变化而被破坏,而没有自定义比较功能。

Should operator< for std::string be affected by current locale?

没有。运算符在内部使用 std::string::compare。这又使用 std::string::traits_type 进行比较。 std::string::traits_typestd::char_traits<char>,它与语言环境无关。

Conclusion, the locale affects the comparison between a and b.

无法得出这样的结论,因为断言之间的顺序保持不变。

你的结论是错误的:http://coliru.stacked-crooked.com/a/38971b91c881da2c

a>b 等同于 b<a,你肯定是想用 a<bb>a 来表示第二个 assert