Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang std::isspace compilation error

Following code compiles fine on VS 2015(Update 3) and gcc 6.3 (C++14) without on any problems.

#include <string>
#include <locale>

int main()
{
    std::u16string ustr = u"Android";

    bool var = std::isspace(ustr[0],std::locale());

    return 0;
}

However, on clang/Xcode it fails with following error

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.

What am I missing here ? Any header inclusion ? If so, which one is it ? If not, any other workarounds to solve this ?

like image 482
Recker Avatar asked Oct 22 '25 05:10

Recker


1 Answers

The std::isspace overload in <locale> is equivalent (per the standard) to:

std::use_facet< std::ctype<charT> >(loc).is(ctype_base::space, c)

As you can see, this requires a specialization of std::ctype for the given CharT.

The standard only provides std::ctype<char> and std::ctype<wchar_t>. Since char16_t is a built-in type, you cannot specialize std::ctype<char16_t> so I think you are screwed here.

like image 105
Holt Avatar answered Oct 24 '25 16:10

Holt