[英]How to force compiler to deduce a class template argument?
我是C ++模板的新手,但我(很多失败)试图强制编译器在初始化时推断出模板typename参数。
这是我的代码。
template <typename T>
class C
{
public:
T a;
C(T a) : a(a) {}
C(const C<T>& other) : a(other.a) {}
};
int main()
{
C<int> x(1);
C y{ x };
return 0;
}
由g ++编译的该代码将导致错误。
test.cpp:13:11: error: missing template arguments before ‘y’
C y{ x };
^
我想保持这种语法-只是C,而没有明确指定template参数。
我曾尝试使用演绎指南,但这仅引发了另一个错误。
template <typename T> C(const C<T>& other) -> C<T>;
当我将此行插入C类的定义下方时,我得到了。
test.cpp:10:51: error: expected constructor, destructor, or type conversion before ‘;’ token
template <typename T> C(const C<T>& other) -> C<T>;
^
当我将此行放入C类定义(在顶部)时,发生了另一个错误。
C(const C<T>& other) -> C<T>;
test.cpp:4:26: error: ‘C’ function with trailing return type not declared with ‘auto’ type specifier
C(const C<T>& other) -> C<T>;
^~~~
在这两种情况下,仍然存在首次提及错误。
谢谢!
在C ++ 11中,仅模板函数可以推导其模板参数。 例如,给定:
void f(std::pair<int, char> &s);
,有人可以这样称呼它
f(std::make_pair(5, 'c'));
,因为std::make_pair
是一个函数,可以为函数推导模板参数。
但是,用一对来称呼是非法的:
f(std::pair(5, 'c'));
,因为类模板没有模板参数推导。 此问题已在C ++ 17中修复,使std::make_pair
有点过时了。
有关新类模板参数推导的更多信息,请参见此处 。
为了解决您的问题,在使用C ++ 11使用gcc进行编译时,我已经遇到了相同的错误。 代码使用C ++ 17编译时没有错误(对于gcc,可以通过传递-std=c++17
参数来完成)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.