模板默认参数 [英] Template default arguments
本文介绍了模板默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我允许执行以下操作:
If I am allowed to do the following:
template <typename T = int>
class Foo{
};
为什么我不允许在主体中执行以下操作?
Why am I not allowed to do the following in main?
Foo me;
但我必须指定以下内容:
But I must specify the following:
Foo<int> me;
C ++ 11引入了默认模板参数,现在他们难以理解。 / p>
C++11 introduced default template arguments and right now they are being elusive to my complete understanding.
推荐答案
您必须:
Foo<> me;
模板参数必须存在,但您可以将它们留空。
The template arguments must be present but you can leave them empty.
想像一个带有单个默认参数的函数 foo
。表达式 foo
不会调用它,但 foo()
会。参数语法必须仍然存在。这是一致的。
Think of it like a function foo
with a single default argument. The expression foo
won't call it, but foo()
will. The argument syntax must still be there. This is consistent with that.
这篇关于模板默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文