Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 明确专业化';CheckIntMap<&燃气轮机';实例化后_C++_Templates_Template Specialization_Explicit Specialization - Fatal编程技术网

C++ 明确专业化';CheckIntMap<&燃气轮机';实例化后

C++ 明确专业化';CheckIntMap<&燃气轮机';实例化后,c++,templates,template-specialization,explicit-specialization,C++,Templates,Template Specialization,Explicit Specialization,编译错误: Apple LLVM version 9.0.0 (clang-900.0.39.2) Target: x86_64-apple-darwin17.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin Found CUDA installation: /usr/local/cuda, version 7.0 /Users/xxx/xxx/xxxx/common/src/co

编译错误:

Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Found CUDA installation: /usr/local/cuda, version 7.0
/Users/xxx/xxx/xxxx/common/src/constexprfunc.hpp:58:30:错误:实例化后“CheckIntMap”的显式专门化
模板constexpr int CheckIntMap(const char*szStr,int nDefaultInt,const char*szOptStr1,int nOptInt1)
^
/Users/xxx/xxx/xxxx/common/src/constexprfunc.hpp:55:17:注意:这里首先需要隐式实例化
返回(CheckIntMap(szStr,nDefaultInt,szOptStr1,nOptInt1)=nOptInt1)?nOptInt1:CheckIntMap(szStr、nDefaultInt、std::forward(u)…);
这段代码是用g++5.4(linux)编译的


如何修复此问题。

您可以将专门化声明移动到需要隐式实例化的位置之前。e、 g

/Users/xxx/xxx/xxxx/common/src/constexprfunc.hpp:58:30: error: explicit specialization of 'CheckIntMap<>' after instantiation
    template<> constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1)
                             ^
/Users/xxx/xxx/xxxx/common/src/constexprfunc.hpp:55:17: note: implicit instantiation first required here
        return (CheckIntMap(szStr, nDefaultInt, szOptStr1, nOptInt1) == nOptInt1) ? nOptInt1 : CheckIntMap(szStr, nDefaultInt, std::forward<U>(u)...);
//声明
模板constexpr int CheckIntMap(const char*szStr,int nDefaultInt,const char*szOptStr1,int nOptInt1,U&&…U);
模板constexpr int CheckIntMap(const char*szStr,int nDefaultInt,const char*szOptStr1,int nOptInt1);
//定义
模板constexpr int CheckIntMap(const char*szStr,int nDefaultInt,const char*szOptStr1,int nOptInt1,U&&…U)
{
返回(CheckIntMap(szStr,nDefaultInt,szoptsttr1,nOptInt1)==nOptInt1)?nOptInt1:CheckIntMap(szStr,nDefaultInt,std::forward(u)…);
}
模板constexpr int CheckIntMap(const char*szStr,int nDefaultInt,const char*szOptStr1,int nOptInt1)
{
返回CompareUTF8(szStr,szOptStr1)?nOptInt1:nDefaultInt;
}

从第二个定义中删除
模板后,它会引发
错误:调用“CheckIntMap”时没有匹配的函数。
@CI删除了我之前的注释,因为它是错误的(对不起):您可以专门化函数模板,但可能不建议这样做,因为如果您也重载它,它会以一种奇怪的方式与重载解析交互,而重载更常见(cf)。删除模板(并将专门化转换为重载)后出现的错误肯定是因为在第一个模板中使用第二个重载之前没有声明第二个重载。你的问题已经解决了,但是我想我应该添加一些额外的解释。非常感谢你,它修复了在定义之前放置声明之后的编译。顺便问一下,这不是定义模板函数的标准吗?将声明放在定义之前,这不适用于普通函数。@Ryanching专门化和实例化对于模板是特殊的,但基本思想是相同的;当你想调用一个函数(在这种情况下是专门化)时,应该在调用之前声明它。
/Users/xxx/xxx/xxxx/common/src/constexprfunc.hpp:58:30: error: explicit specialization of 'CheckIntMap<>' after instantiation
    template<> constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1)
                             ^
/Users/xxx/xxx/xxxx/common/src/constexprfunc.hpp:55:17: note: implicit instantiation first required here
        return (CheckIntMap(szStr, nDefaultInt, szOptStr1, nOptInt1) == nOptInt1) ? nOptInt1 : CheckIntMap(szStr, nDefaultInt, std::forward<U>(u)...);
// declarations
template<typename... U> constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1, U&&... u);
template<> constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1);

// definitions
template<typename... U> constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1, U&&... u)
{
    return (CheckIntMap(szStr, nDefaultInt, szOptStr1, nOptInt1) == nOptInt1) ? nOptInt1 : CheckIntMap(szStr, nDefaultInt, std::forward<U>(u)...);
}

template<> constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1)
{
    return CompareUTF8(szStr, szOptStr1) ? nOptInt1 : nDefaultInt;
}