以下内容无法编译:
struct S {
template <class T> S(T) {}
};
void f(int) {}
int main() {
S(f);
}
g ++-4.9说
template.cpp: In function ‘int main()’:
template.cpp:6:8: error: no matching function for call to ‘S::S()’
S(f);
^
template.cpp:6:8: note: candidates are:
template.cpp:2:24: note: template<class T> S::S(T)
template <class T> S(T) {}
^
template.cpp:2:24: note: template argument deduction/substitution failed:
template.cpp:6:8: note: candidate expects 1 argument, 0 provided
S(f);
^
template.cpp:1:8: note: constexpr S::S(const S&)
struct S {
^
template.cpp:1:8: note: candidate expects 1 argument, 0 provided
template.cpp:1:8: note: constexpr S::S(S&&)
template.cpp:1:8: note: candidate expects 1 argument, 0 provided
铛给出了类似的错误。
另一方面,将编译以下内容:
struct S {
template <class T> S(T) {}
};
void f(int) {}
int main() {
S s = S(f); // this line was changed
}
那么这是怎么回事?
问题
您编写的代码并不代表您的实际意思,实际上是在声明一个S
具有名称的类型的变量f
;它不是S
使用f
作为参数构造type的未命名实体。
注意:当您将行更改S s = S(f)
为时,您将声明一个名为s
type的变量S
,并使用临时初始化S(f)
(即,将使用S的副本构造函数进行初始化)。s
解决方案
将类型包装在括号中,或使用统一初始化(在C ++ 11中引入)。
(S) (f); // creates a temporary of type S initialized with `f`
S { f }; // c++11
原因
标准(n3797)表示T(x)
等同于T x
声明变量,如以下部分所述:
8.3p6声明符的含义
[dcl.meaning]
在声明
T D
中D
具有以下形式( D1 )
所包含的说明符-ID的类型是一样的包含的说明符-ID的声明
T D1
括号不会更改嵌入的声明符id的类型,但可以更改复杂声明符的绑定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句