Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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

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_Compiler Errors - Fatal编程技术网

C++ 调用参数可隐式转换为模板类对象的函数

C++ 调用参数可隐式转换为模板类对象的函数,c++,templates,compiler-errors,C++,Templates,Compiler Errors,考虑以下示例(): 模板 结构{ S(int){} }; 模板 空f(S,T){} int main(){ f(1,2); } 编译它会出现以下错误: <source>: In function 'int main()': 10 : <source>:10:11: error: no matching function for call to 'f(int, int)' f(1, 2); ^ 7 : <source>:7:6:

考虑以下示例():

模板
结构{
S(int){}
};
模板
空f(S,T){}
int main(){
f(1,2);
}
编译它会出现以下错误:

<source>: In function 'int main()':
10 : <source>:10:11: error: no matching function for call to 'f(int, int)'
     f(1, 2);
           ^
7 : <source>:7:6: note: candidate: template<class T> void f(S<T>, T)
 void f(S<T>, T) {}
      ^
7 : <source>:7:6: note:   template argument deduction/substitution failed:
10 : <source>:10:11: note:   mismatched types 'S<T>' and 'int'
     f(1, 2);
           ^
:在函数“int main()”中:
10::10:11:错误:对“f(int,int)”的调用没有匹配的函数
f(1,2);
^
7:7:6:注:候选人:模板无效f(S,T)
空f(S,T){}
^
7::7:6:注意:模板参数扣除/替换失败:
10::10:11:注意:类型“S”和“int”不匹配
f(1,2);
^
使
S
成为非模板会使示例编译


尽管从
int
S
进行了隐式转换,但为什么此代码不编译?

模板函数不是函数。它们是编写函数的模板

template <typename T>
void f(S<T>, T) {}

现在,您的主编译。

T
无法在
S
中推导。这可能看起来很奇怪,但是考虑一下只有一个转换,从<代码> int >代码>到<代码> s >代码>,<代码> t>代码>应该是什么?或者也许<代码> s >代码>没有构造注意到<代码> f(1, 2)< /代码>。
template <typename T>
void f(S<T>, T) {}
template<class T>struct tag_t{using type=T;}:
template<class T>using block_deduction=typename tag_t<T>::type;

template <typename T>
void f(block_deduction<S<T>>, T) {}