Rec*_*ker 3 c++ c++11 clang++ c++14
以下代码在VS 2015(更新3)和gcc 6.3(C++ 14)上编译正常,没有任何问题.
#include <string>
#include <locale>
int main()
{
std::u16string ustr = u"Android";
bool var = std::isspace(ustr[0],std::locale());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,在clang/Xcode上它失败并出现以下错误
Error(s):
source_file.cpp:8:10: warning: unused variable 'var' [-Wunused-variable]
bool var = std::isspace(ustr[0],std::locale());
^
In file included from source_file.cpp:2:
In file included from /usr/include/c++/v1/locale:182:
/usr/include/c++/v1/__locale:705:44: error: implicit instantiation of undefined template 'std::__1::ctype<char16_t>'
return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
^
source_file.cpp:8:21: note: in instantiation of function template specialization 'std::__1::isspace<char16_t>' requested here
bool var = std::isspace(ustr[0],std::locale());
^
/usr/include/c++/v1/__locale:427:53: note: template is declared here
template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
^
/usr/include/c++/v1/__locale:186:54: error: implicit instantiation of undefined template 'std::__1::ctype<char16_t>'
return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
^
/usr/include/c++/v1/__locale:705:12: note: in instantiation of function template specialization 'std::__1::use_facet<std::__1::ctype<char16_t> >' requested here
return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
^
source_file.cpp:8:21: note: in instantiation of function template specialization 'std::__1::isspace<char16_t>' requested here
bool var = std::isspace(ustr[0],std::locale());
^
/usr/include/c++/v1/__locale:427:53: note: template is declared here
template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
^
1 warning and 2 errors generated.
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?任何标题包含?如果是这样,那是哪一个?如果没有,还有其他解决方法可以解决这个问题吗?
的std::isspace在过载<locale>是(按标准),以当量:
std::use_facet< std::ctype<charT> >(loc).is(ctype_base::space, c)
Run Code Online (Sandbox Code Playgroud)
如您所见,这需要std::ctype针对给定的特化CharT.
该标准仅提供std::ctype<char>和std::ctype<wchar_t>.由于char16_t是内置类型,你不能专业,std::ctype<char16_t>所以我认为你搞砸了.
问题在于 的std::locale实现std::isspace使用 中定义的字符特征std::ctype,这些特征定义了字符类型的某些特征。
不幸的是,该标准只要求std::ctype专门用于char和wchar_t,而不是char16_t像您所需要的那样。看来 MSVC 和 GCC 正在提供额外的实现(因为这很有意义)。(编辑:GCC 实际上抛出了一个异常)。
我们无法为其添加自己的专业化,因为 [namespace.std] 声明我们只能将用户定义类型的专业化添加到namespace std.
这给我们留下了几个选择:
char或之前将您的角色投射到wchar_tstd::isspace<locale>
char这通常会是您可能不希望的缩小转换std::isspace切换到使用from的实现来<cctype>强制转换为int.
unsigned char,则您有未定义的行为,因此这也可能不是您想要的,具体取决于您使用的类型(检查sizeof(unsigned char))@Holt,并接受未定义的行为