模板参数扣除构造函​​数 [英] template argument deduction for constructors

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

问题描述

C ++ 0x有(或者是C ++ 0x在某个时间点有)模板参数扣除构造函​​数?在即将推出的C ++(C ++ 0x)标准概述中,我看到了以下几行:

Does C++0x have (or was C++0x at some point in time going to have) template argument deduction for constructors? In An Overview of the Coming C++ (C++0x) Standard, I saw the following lines:

std::lock_guard l(m);   // at 7:00

std::thread t(f);       // at 9:00

这意味着委托 make_foo 函数模板最终是多余的?

Does this mean that delegating make_foo function templates are finally redundant?

推荐答案

模板参数扣除适用于任何函数,包括构造函数。但是你不能从传递给构造函数的参数中推导出类模板参数。不,你不能在
C ++ 0x中做。

Template argument deduction works for any function, including the constructor. But you can't deduce the class template parameters from arguments passed to the constructor. And no, you can't do it in C++0x either.

struct X
{
    template <class T> X(T x) {}
};

template <class T>
struct Y
{
    Y(T y) {} 
};

int main()
{
   X x(3); //T is deduced to be int. OK in C++03 and C++0x; 
   Y y(3); //compiler error: missing template argument list. Error in 03 and 0x
}

lock_guard 线程不是类模板。他们有构造函数模板。

lock_guard and thread aren't class templates. They have constructor templates though.

这篇关于模板参数扣除构造函​​数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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