Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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++ 函数中推导参数之前的默认模板参数?_C++_Templates_C++11 - Fatal编程技术网

C++ 函数中推导参数之前的默认模板参数?

C++ 函数中推导参数之前的默认模板参数?,c++,templates,c++11,C++,Templates,C++11,以下代码在g++中编译时没有问题: template<typename ReturnType = double, typename OtherType> ReturnType func(const OtherType& var) { ReturnType result = 0; /* SOMETHING */ return result; } 模板返回类型func(const OtherType&var) { 返回类型结果=0; /*某物*/ 返回结

以下代码在g++中编译时没有问题:

template<typename ReturnType = double, typename OtherType> ReturnType func(const OtherType& var)
{
    ReturnType result = 0;
    /* SOMETHING */
    return result;
}
模板返回类型func(const OtherType&var)
{
返回类型结果=0;
/*某物*/
返回结果;
}

所有符合标准的编译器在默认模板参数(
ReturnType
此处)之后都有一个非默认模板参数(
OtherType
此处)可以吗?

这很复杂。从C++11规范:

如果类模板的模板参数具有默认模板参数,则每个后续模板参数应具有提供的默认模板参数或模板参数包。如果主类模板的模板参数是模板参数包,则应为最后一个模板-参数。[注:这些不是函数模板或类模板部分专门化的要求,因为可以推导模板参数(14.8.2)

所以,除非是部分专门化,否则类是不允许这样做的,但函数是可以的

所以,只要你只使用你在例子中展示的函数来做这个把戏,就可以了。你不能把它推广到类模板