构造函数的模板参数推导

时间:2011-08-07 09:35:17

标签: c++ templates constructor c++11 type-inference

C ++ 0x是否有(或者在某个时间点是C ++ 0x)构造函数的模板参数推导?在An Overview of the Coming C++ (C++0x) Standard中,我看到了以下几行:

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

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

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

1 个答案:

答案 0 :(得分:14)

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

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_guardthread不是类模板。他们有构造函数模板。

相关问题