类和函数在C ++中的模板类型扣除? [英] Template type deduction in C++ for Class vs Function?

查看:104
本文介绍了类和函数在C ++中的模板类型扣除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么自动类型扣除只对函数而不对类才可能?

Why is that automatic type deduction is possible only for functions and not for Classes?

推荐答案

std :: make_pair

template<class T>
make_foo(T val) {
    return foo<T>(val);
}

EDIT: C ++编程语言,第三版,第335页。Bjarne说:

I just found the following in "The C++ Programming Language, Third Edition", page 335. Bjarne says:


注意类模板参数是
从来没有推导过。原因是由几个
构造函数为一个类提供的
灵活性将使
在许多
的情况下不可能进行这样的扣除,并且在更多的情况下模糊。

Note that class template arguments are never deduced. The reason is that the flexibility provided by several constructors for a class would make such deduction impossible in many cases and obscure in many more.

这当然是非常主观的。在 comp.std.c ++ 中有一些讨论,并且一致似乎是没有理由不能支持 。是否一个好主意是否是另一个问题...

This is of course very subjective. There's been some discussion about this in comp.std.c++ and the consensus seems to be that there's no reason why it couldn't be supported. Whether it would be a good idea or not is another question...

这篇关于类和函数在C ++中的模板类型扣除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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