C++ MacOSX中带有clang的函数模板专门化

C++ MacOSX中带有clang的函数模板专门化,c++,templates,C++,Templates,我正在将一些代码从Windows移植到MacOSX。函数模板专门化有一个问题。 我使用msvc14.0和clang5.0在Windows中成功编译了此文件。 #include <cstring> #include <string> #include <iostream> template<typename CHAR_TYPE> size_t string_length(CHAR_TYPE text) { static_assert(fa

我正在将一些代码从Windows移植到MacOSX。函数模板专门化有一个问题。 我使用msvc14.0和clang5.0在Windows中成功编译了此文件。

#include <cstring>
#include <string>

#include <iostream>

template<typename CHAR_TYPE>
size_t string_length(CHAR_TYPE text)
{
    static_assert(false, "string_length is not supported this type.");
    return 0;
}

template<typename CHAR_TYPE>
size_t string_length(const std::basic_string<CHAR_TYPE>& text)
{
    return text.size();
}

template<>
inline size_t string_length(const char* szText)
{
    return strlen(szText);
}

template<>
inline size_t string_length(const wchar_t* szwText)
{
    return wcslen(szwText);
}

int main()
{
    std::cout << "SZ length " << string_length("IIII") << std::endl;
    std::cout << "WSZ length " << string_length(L"IIII") << std::endl;
    std::cout << "string length " << string_length(std::string("IIII")) << std::endl;
    std::cout << "wstring length " << string_length(std::wstring(L"IIII")) << std::endl;
}
当我删除第一个函数时:

template<typename CHAR_TYPE>
size_t string_length(CHAR_TYPE text)
{
    static_assert(false, "string_length is not supported this type.");
    return 0;
}
模板
大小字符串长度(字符类型文本)
{
static_assert(false,“此类型不支持字符串长度”);
返回0;
}
我还有一些错误:

clang_test.cpp:19:15: error: no function template matches function template
      specialization 'string_length'
inline size_t string_length(const char* szText)
              ^
clang_test.cpp:13:8: note: candidate template ignored: could not match 'const
      basic_string<type-parameter-0-0, char_traits<type-parameter-0-0>,
      allocator<type-parameter-0-0> > &' against 'const char *'
size_t string_length(const std::basic_string<CHAR_TYPE>& text)
       ^
clang_test.cpp:25:15: error: no function template matches function template
      specialization 'string_length'
inline size_t string_length(const wchar_t* szwText)
              ^
clang_test.cpp:13:8: note: candidate template ignored: could not match 'const
      basic_string<type-parameter-0-0, char_traits<type-parameter-0-0>,
      allocator<type-parameter-0-0> > &' against 'const wchar_t *'
size_t string_length(const std::basic_string<CHAR_TYPE>& text)
       ^
clang_test.cpp:33:34: error: no matching function for call to 'string_length'
    std::cout << "SZ length " << string_length("IIII") << std::endl;
                                 ^~~~~~~~~~~~~
clang_test.cpp:13:8: note: candidate template ignored: could not match
      'basic_string<type-parameter-0-0, char_traits<type-parameter-0-0>,
      allocator<type-parameter-0-0> >' against 'char const[5]'
size_t string_length(const std::basic_string<CHAR_TYPE>& text)
       ^
clang_test.cpp:34:35: error: no matching function for call to 'string_length'
    std::cout << "WSZ length " << string_length(L"IIII") << std::endl;
                                  ^~~~~~~~~~~~~
clang_test.cpp:13:8: note: candidate template ignored: could not match
      'basic_string<type-parameter-0-0, char_traits<type-parameter-0-0>,
      allocator<type-parameter-0-0> >' against 'wchar_t const[5]'
size_t string_length(const std::basic_string<CHAR_TYPE>& text)
       ^
4 errors generated.
clangu测试。cpp:19:15:错误:没有与函数模板匹配的函数模板
专门化“字符串长度”
内联大小\u t字符串长度(常量字符*szText)
^
clang_test.cpp:13:8:注意:已忽略候选模板:无法匹配“const”
基本字符串&'与'const char*'
大小字符串长度(常量标准::基本字符串和文本)
^
clang_test.cpp:25:15:错误:没有与函数模板匹配的函数模板
专门化“字符串长度”
内联大小字符串长度(常量wchar*szwText)
^
clang_test.cpp:13:8:注意:已忽略候选模板:无法匹配“const”
基本字符串与常量wchar\u t*相对
大小字符串长度(常量标准::基本字符串和文本)
^
clang_test.cpp:33:34:错误:调用“string_length”时没有匹配函数

std::cout即使函数未实例化,以下内容也确实是错误的:

template<typename CHAR_TYPE>
size_t string_length(CHAR_TYPE text)
{
    static_assert(false, "string_length is not supported this type.");
    return 0;
}
如果希望保持
静态\u断言
,则必须创建一个帮助器:

template <typename T> struct always_false : false_type {};

template<typename CHAR_TYPE>
size_t string_length(CHAR_TYPE text)
{
    static_assert(always_false<CHAR_TYPE>::value,
                  "string_length is not supported this type.");
    return 0;
}
模板结构总是\u false:false\u类型{};
模板
大小字符串长度(字符类型文本)
{
静态断言(始终为假::值,
“此类型不支持字符串长度。”);
返回0;
}

即使函数未实例化,以下内容也确实是错误的:

template<typename CHAR_TYPE>
size_t string_length(CHAR_TYPE text)
{
    static_assert(false, "string_length is not supported this type.");
    return 0;
}
如果希望保持
静态\u断言
,则必须创建一个帮助器:

template <typename T> struct always_false : false_type {};

template<typename CHAR_TYPE>
size_t string_length(CHAR_TYPE text)
{
    static_assert(always_false<CHAR_TYPE>::value,
                  "string_length is not supported this type.");
    return 0;
}
模板结构总是\u false:false\u类型{};
模板
大小字符串长度(字符类型文本)
{
静态断言(始终为假::值,
“此类型不支持字符串长度。”);
返回0;
}

我不认为这是适用的子弹。但是,是的,[temp.res]/8适用于90%的非法模板定义。@StoryTeller:经过编辑,也包含了您的项目符号,这确实更合适。我认为这不是适用的项目符号。但是,是的,[temp.res]/8用于90%的非法模板定义。@StoryTeller:编辑后也包含了您的项目符号,这确实更合适。