使用模板模板参数时出现荒谬的错误
我一直在尝试创建一个模板类(Test2
),它采用 2 个模板参数,Type1
和 Type2
。众所周知,第二个参数也是一个模板类,它采用 2 个模板参数(TypeA
和 TypeB
)。
现在,为了构造 Test2
的对象,我希望用户能够使用两种类型的构造函数之一:
- 一个采用
Type1
和Type2 的对象
。 - 一个接受
Type1
、TypeA
和TypeB
的对象。
我编写了以下代码:
#include <iostream>
template<class TypeA, class TypeB>
struct Test
{
TypeA t1obj;
TypeB t2obj;
Test(const TypeA& t1, const TypeB& t2)
: t1obj(t1), t2obj(t2) {std::cout<<"Test::Type1, Type2\n";}
};
template<class Type1,
template<typename TypeX, typename TypeY> class Type2 >
struct Test2
{
Type1 t1obj;
Type2<typename TypeX, typename TypeY> t2obj; //Line 17
Test2(const Type1& t1,
const Type2<typename TypeX, typename TypeY>& t2) //Line 20
: t1obj(t1), t2obj(t2) { std::cout<<"Test2::Type1, Type2\n";}
Test2(const Type1& t1,
const TypeX& x,
const TypeY& y)
: t1obj(t1), t2obj(x,y) { std::cout<<"Test2::Type1, X, Y\n";}
};
int main()
{
Test<int, char> obj1(1,'a');
Test2<int, Test<int, char> > strangeobj1(10,obj1);
Test2<int, Test<int, char> > strangeobj2(1,2,'b');
}
我已经尝试了很多,但遇到了非常荒谬的错误,例如:
第 17 行和第 20 行上的模板参数数量错误(1,应该是 2)。
I have been trying to create a templated class(Test2
) that takes 2 template arguments,Type1
and Type2
. It is known that the second argument would also be a templated class that takes 2 template arguments(TypeA
and TypeB
).
Now, for constructing an object of Test2
, I want the user to be able to use either of 2 types of constructors:
- One that takes objects of
Type1
andType2
. - One that takes objects of
Type1
,TypeA
andTypeB
.
I wrote the following code:
#include <iostream>
template<class TypeA, class TypeB>
struct Test
{
TypeA t1obj;
TypeB t2obj;
Test(const TypeA& t1, const TypeB& t2)
: t1obj(t1), t2obj(t2) {std::cout<<"Test::Type1, Type2\n";}
};
template<class Type1,
template<typename TypeX, typename TypeY> class Type2 >
struct Test2
{
Type1 t1obj;
Type2<typename TypeX, typename TypeY> t2obj; //Line 17
Test2(const Type1& t1,
const Type2<typename TypeX, typename TypeY>& t2) //Line 20
: t1obj(t1), t2obj(t2) { std::cout<<"Test2::Type1, Type2\n";}
Test2(const Type1& t1,
const TypeX& x,
const TypeY& y)
: t1obj(t1), t2obj(x,y) { std::cout<<"Test2::Type1, X, Y\n";}
};
int main()
{
Test<int, char> obj1(1,'a');
Test2<int, Test<int, char> > strangeobj1(10,obj1);
Test2<int, Test<int, char> > strangeobj2(1,2,'b');
}
I have tried a lot but I get really absurd errors like:
wrong number of template arguments (1, should be 2)
on Line 17 and 20.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不是那样工作的。
Test
是一个完整的类型,而不是模板。因此,您需要类型参数,为了获取
TypeX
和TypeY
,导出它们很有用,这样您就可以在Test2
中使用它们,如上所示。请务必阅读 放置“模板”的位置和依赖名称上的“typename”,以了解为什么以及何时在类型名称之前使用
typename
,如上所示。It doesn't work like that.
Test<int, char>
is a full blown type, instead of a template. So you need type parametersFor getting
TypeX
andTypeY
it's useful to export them so you can use them inTest2
as shown above.Be sure to read Where to put the "template" and "typename" on dependent names to understand why and when to use
typename
before type names like above.这有几个错误,但主要错误似乎
不是传递模板模板参数的方式。这将使用
That is because
Test
is a template butTest
is a type 来传递(从该模板生成。)There are several errors with this, but the main error seems to be that
is not how you pass a template template parameter. This would be passed using
That is because
Test
is a template butTest<int, char>
is a type (generated from that template.)Type1
是类型,Type2
是模板。您认为TypeX
和TypeY
到底定义了什么?在template行内class Type2 >
,它们将被忽略。Type1
is a type,Type2
is a template. What exactly do you thinkTypeX
andTypeY
are defined? Inside the linetemplate<typename TypeX, typename TypeY> class Type2 >
, they are ignored.这是一种选择:
Here's one option:
Test
与template不匹配class Type2
第一个是模板类的实例化,它不接受任何参数。第二个是接受两个参数的模板类模式。
Test<int, char>
is not a match fortemplate<typename TypeX, typename TypeY> class Type2
The first one is an instantiation of a template class, it does not accept any parameters. The second one is a template class pattern accepting two parameters.