构造函数的模板参数推导

fre*_*low 8 c++ templates constructor type-inference c++11

C++ 0x是否具有(或者某个时间点的C++ 0x)构造函数的模板参数推导?在Coming C++(C++ 0x)标准概述中,我看到以下几行:

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

std::thread t(f);       // at 9:00
Run Code Online (Sandbox Code Playgroud)

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

Arm*_*yan 15

模板参数推导适用于任何函数,包括构造函数.但是你不能从传递给构造函数的参数中推断出类模板参数.不,你不能在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
}
Run Code Online (Sandbox Code Playgroud)

lock_guardthread不是类模板.他们有构造函数模板.

  • 你的帖子是在2011年,但为了更新一点C++ 1y应该很快就会有:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3836.html看看论文页面上的论文N3602(http://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3602.html). (2认同)