我不知道为什么这些代码无法编译.我在Visual c + + 2010和gcc中测试了-std = c ++ 0x.有人提出一些建议吗?谢谢!
template<typename T>
class Foo
{
public:
void test(const T&){cout<<"const";}
void test( T&){cout<<"non const";}
};
int main()
{
int a;
Foo<int&> f;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:'void Foo :: test(T)':已定义或声明的成员函数
但为什么这可以编译?
template<typename T> void foo(const T&){cout<<"const"; }
template<typename T> void foo( T&){cout<<"non const"; }
int main()
{
int a;
foo<int&>(a);
}
Run Code Online (Sandbox Code Playgroud)
我读过c ++ 0x文章说:T && == T&,所以const T && == const T&?
我想得到lambda的类型作为模板参数.这该怎么做?
template<class T>
class Foo
{
public:
Foo(T t){ t(); }
};
int main()
{
Foo< type?? > foo([](){ cout<<"construct OK"; });
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)