简体   繁体   English

需要有关模板 class 实例化的帮助

[英]Need help regarding Template class instantiation

Let me present my problem with an example:让我用一个例子来说明我的问题:

template <typename T> class a{
public:
    T data;
    a():data(T()){}
    a(T temp): data(temp) {}
};

So if write in main() like所以如果写在main()之类的

a(30);
a("String");

So according to the template argument deduction rule, it should be able to generate the first temporary class as a<int>(30) etc所以根据模板参数推导规则,应该可以生成第一个临时的class as a<int>(30)

But I the error which says:但我的错误是:

missing template arguments before '(' token '(' 标记之前缺少模板 arguments

so why this happens, this is only true for function template?那么为什么会发生这种情况,这仅适用于 function 模板?

Template parameter deduction from arguments only works for functions , never for classes . arguments 的模板参数推导仅适用于函数,不适用于 Until you know the type of the class, ie all its template parameters, you don't even know which member functions the class has!在您知道 class 的类型,即它的所有模板参数之前,您甚至不知道 class 有哪些成员函数!

So, you always have to say the template parameters if you want to construct an object directly:所以,如果你想直接构造一个 object,你总是要说模板参数:

a<int> x(30);

Here's a little thought experiment to expand on the above.这里有一个小思想实验来扩展上述内容。 Suppose we have假设我们有

template <typename T> class Foo;

and we are calling Foo::somefunction(x);我们正在调用Foo::somefunction(x); , where x is some type. ,其中x是某种类型。 You think, well, I declared somefunction() like this:你认为,好吧,我声明somefunction()是这样的:

template <typename T> class Foo
{
  static void somefunction(const T & x);
};

so it should be obvious that T is the same type as the type of x .所以很明显Tx的类型相同。 But now imagine I have a specialization:但现在想象一下我有一个专业:

template <> class Foo<char>
{
  static void anotherfunction(double x);
};

The class Foo<char> doesn't even have a function somefunction() , so the expression Foo::somefunction(x) doesn't even get to the stage where I could look up the argument! class Foo<char>甚至没有function somefunction() ,所以表达式Foo::somefunction(x)甚至没有达到我可以查找参数的阶段!

The usual way around this is to make a free helper function that constructs your object:解决这个问题的常用方法是创建一个免费的助手 function 来构建您的 object:

template <typename T> a<T> make_a(const T & x) { return a<T>(x); }

Since this is a function template, its parameters can be deduced:由于这是一个function模板,所以可以推导出它的参数:

make_a(30);      // type a<int>
make_a("hello"); // type a<char[6]>

The constructor is not a template, its the class which is a template.构造函数不是模板,它的 class 是模板。 So when you write a(30) , the template argument deduction for the class template cannot be done!所以当你写a(30)时,不能对 class 模板进行模板参数推导!

If there exists a constructor template, then the template argument for the templated constructor can be deduced by the compiler.如果存在构造函数模板,则编译器可以推导出模板化构造函数的模板参数。 For example here:例如这里:

template <typename T> class A{
public:
    template<typename U>
    A(const U &): {}   //note : it's a constructor template
};

A<char>  obj(30); //U is deduced as int

In the above example, only U can be deduced, you still have to provide T .在上面的例子中,只能推导出U ,你仍然需要提供T Its because这是因为

  • U is a template argument for the constructor template. U 是构造函数模板的模板参数。 Template argument deduction can be done in this case.在这种情况下可以进行模板参数推导。
  • T is a template argument for the class template. T 是 class 模板的模板参数。 Template argument deduction cannot be done here.此处不能进行模板参数推导。

You still need to declare a temporary as, for example, a<int>(30) .您仍然需要将临时声明为例如a<int>(30)

You cannot infer class template arguments from the arguments to the constructor- unfortunately.您不能将 class 模板 arguments 从 arguments 推断到构造函数 - 不幸的是。

Template type deduction only happens for template functions.模板类型推导只发生在模板函数上。 You need to specify the parameters for a template class instantiation.您需要为模板 class 实例化指定参数。 You can use a function template to deduce the template parameter and return the appropriate type.您可以使用 function 模板来推导模板参数并返回适当的类型。 In c++0 x you could use auto to hold the instance.在 c++0 x 中,您可以使用 auto 来保存实例。 Can't easily write example code for you on my phone!无法在我的手机上轻松为您编写示例代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM