为什么构造函数无法推断出模板参数? [英] Why can't constructors deduce template arguments?

查看:117
本文介绍了为什么构造函数无法推断出模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template< class T >
class Foo {
public:
  Foo( T t ) { }
};

int main () {
  int i = 0;
  Foo f( i );
}

在上面的代码中,编译器抱怨模板参数在'f之前丢失'。我知道,从构造函数的参数推导出类的模板参数不是标准的一部分,但是我的问题是为什么?编译器不具备隐式实例化 Foo< int> 并调用其构造函数所需的全部信息吗?

In the above code, the compiler complains that template arguments are missing before 'f'. I understand that deducing template arguments for a class from the arguments to the constructor is not part of the standard, but my question is why? Doesn't the compiler have all the information it needs to implicitly instantiate Foo<int> and call its constructor?

经过编辑以明确表明我使用 int 调用构造函数(而不是 short long void * 等)

Edited to make it clear that I'm calling the constructor with an int (as opposed to a short, long, void*, etc.)

推荐答案

因为没有人指定确切的工作方式。标准委员会目前有一项提案,以使其生效。它还列出了一些困难:

Because nobody has specified how exactly that works. There is a current proposal to the standard committee to make it work. It also lists some of the difficulties:

http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4471.html

更新:这是该提案的最新版本:

Update: Here's the newest version of the proposal:

http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/p0091r0.html

这篇关于为什么构造函数无法推断出模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆