[英]"too few template arguments error" when using a template class with default template parameter in c++
I had a class that worked fine.我有一个很好的课程。 I wanted to change it to a template class.
我想将其更改为模板类。 Suppose I have now:
假设我现在有:
template <MyTypes::smallType T = Mytypes::myInt>
class A{....}
Now assume that the class A is used in many other hpp and cpp files, sometimes by including the header file and sometimes by forward declaration as:现在假设类 A 用于许多其他 hpp 和 cpp 文件,有时通过包含头文件,有时通过前向声明为:
template <MyTypes::smallType T> class A;
And it is mostly used as std::shared_ptr<A<>>
in other classes and function calls.它主要用作
std::shared_ptr<A<>>
在其他类和函数调用中。
Now assume that I have another class B which is very similar to A, but used in fewer places (still in other cpp files through forward declaration), and not in a shared_ptr.现在假设我有另一个类 B,它与 A 非常相似,但在较少的地方使用(仍然通过前向声明在其他 cpp 文件中),而不是在 shared_ptr 中。
template <MyTypes::smallType T = Mytypes::myInt>
class B{....}
When trying to compile, I get the error "too few template arguments" for A<>
, but not for B<>
.尝试编译时,我收到
A<>
错误“模板参数太少”,但B<>
。 The project is very large and I do not have a guess in order to produce a simple example with the same problem.该项目非常大,我无法猜测以生成具有相同问题的简单示例。 Could you help me (even by gusses), what might be causing the problem?
你能帮我吗(即使是冒犯了),可能是什么导致了这个问题? Can it be shared_ptr?
可以是shared_ptr吗? Can it be some typedefs in the form of
typedef std::shared_ptr<A<>> APtr
?它可以是
typedef std::shared_ptr<A<>> APtr
形式的一些 typedef 吗? I also get the error "unspecialized class template or generic can't be used as a template or generic argument for template or generic parameter 'param', expected a real type" if that can help with guesses.我还收到错误“非专业化的类模板或泛型不能用作模板或泛型参数 'param' 的模板或泛型参数,应为真实类型”(如果这有助于猜测)。
I would appreciate your help.我会很感激你的帮助。
Quite simply, the forward declaration needs the default parameters.很简单,前向声明需要默认参数。 Simply look at it this way, in simplified form:
简单地这样看,以简化的形式:
template<class T> A;
std::shared_ptr<A<>> a;
When the compiler sees this, that is all the information it has.当编译器看到这一点时,这就是它拥有的所有信息。 So, in this case,
A<>
is obviously missing a type for its template parameters.因此,在这种情况下,
A<>
显然缺少其模板参数的类型。 Now, if you do:现在,如果你这样做:
template<class T = int> A;
std::shared_ptr<A<>> a;
Then the compiler can deduce that A<>
is really A<int>
.然后编译器可以推断出
A<>
确实是A<int>
。
Here's the problem with that though.不过,问题就在这里。 From
[temp.param]/12
来自
[temp.param]/12
A template-parameter shall not be given default arguments by two different declarations in the same scope.
模板参数不应由同一范围内的两个不同声明赋予默认参数。
[ Example: template<class T = int> class X; template<class T = int> class X { /* ... */ }; // error —end example ]
So, if you use a default parameter on your forward declaration, you can't use it on the template itself.因此,如果您在前向声明中使用默认参数,则不能在模板本身上使用它。 Fun times.
娱乐时间。
A cheat to get around this is to have an extra header file which contains just the forward declaration of the template with default params, and all files using the template include that, instead of the full definition of the template (until the full definition is required), including the template's definition.解决这个问题的一个秘诀是有一个额外的头文件,它只包含带有默认参数的模板的前向声明,并且所有使用模板的文件都包含它,而不是模板的完整定义(直到需要完整定义),包括模板的定义。 The template definition would not need the default parameters.
模板定义不需要默认参数。 Not very fun, but it would work.
不是很有趣,但它会起作用。
Alternatively, don't forward declare.或者,不要转发声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.