隐式模板参数
以下代码在 Xcode 中生成编译错误:
template <typename T>
struct Foo
{
Foo(T Value)
{
}
};
int main()
{
Foo MyFoo(123);
return 0;
}
错误:“MyFoo”之前缺少模板参数
将 Foo MyFoo(123);
更改为 Foo
解决了这个问题,但是编译器不应该能够找出适当的数据类型吗?
这是编译器错误,还是我误解了隐式模板参数?
The following code generates a compile error in Xcode:
template <typename T>
struct Foo
{
Foo(T Value)
{
}
};
int main()
{
Foo MyFoo(123);
return 0;
}
error: missing template arguments before 'MyFoo'
Changing Foo MyFoo(123);
to Foo<int> MyFoo(123);
fixes the issue, but shouldn't the compiler be able to figure out the appropriate datatype?
Is this a compiler bug, or am I misunderstanding implicit template parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
理论上,构造函数可以推断出它正在构造的对象的类型,但是语句:
正在为
MyFoo
分配临时空间,并且必须知道MyFoo
的完全限定类型以便了解需要多少空间。如果您想避免键入(即用手指)特别复杂的模板名称,请考虑使用
typedef
:或者在 C++0x 中,您可以使用
auto
关键字让编译器使用类型推断——尽管许多人会认为这会导致代码可读性较差且更容易出错,我自己也是其中之一。 ;pThe constructor could in theory infer the type of the object it is constructing, but the statement:
Is allocating temporary space for
MyFoo
and must know the fully-qualified type ofMyFoo
in order to know how much space is needed.If you want to avoid typing (i.e. with fingers) the name of a particularly complex template, consider using a
typedef
:Or in C++0x you could use the
auto
keyword to have the compiler use type inference--though many will argue that leads to less readable and more error-prone code, myself among them. ;p编译器只能计算出模板函数的模板参数类型,而不能计算出类/结构的模板参数类型
compiler can figure out template parameter type only for templated functions, not for classes/structs
编译器可以推导模板参数这样的情况:
实际上模板参数推导是一个很大的话题。阅读 ACCU 上的这篇文章:
C++ 模板参数推导
Compiler can deduce the template argument such case:
Actually template argument deduction is a huge topic. Read this article at ACCU:
The C++ Template Argument Deduction
在 C++11 中,您可以使用
decltype
:In C++11 you can use
decltype
:这不是一个错误,而是不存在的功能。您必须在实例化期间完全指定类/结构模板参数,始终不会像函数模板那样推断类型。
It's not a bug, it's non-existing feature. You have to fully specify class/structure template arguments during instantiation, always, the types are not inferred as they can be for function templates.
您现在尝试执行的操作可以在 C++ 17 中运行。可以在 C++ 17 中推断模板参数。
What you are trying to do now works in C++ 17. Template parameters can be inferred in C++ 17.
这样很有意义,因为 Foo 不是一个类,只是
Foo
其中 T 是一个类型。在 C++0x 中,您可以使用 auto,并且您可以创建一个函数来使您成为 Foo,我们将其称为 foo(小写 f)。那么你会做
It makes a lot of sense it is like this, as Foo is not a class, only
Foo<T>
where T is a type.In C++0x you can use auto, and you can create a function to make you a Foo, let's call it foo (lower case f). Then you would do