Mir*_*pas 11 c++ templates c++11
是否有可能实现这样的目标:
template<typename Signature>
class Test
{
public:
//here I want operator () to respect the signature
};
Test<void(int)> t1; //void operator()(int)
Test<void(int, float)> t2; //void operator()(int, float)
Run Code Online (Sandbox Code Playgroud)
返回类型总是如此void.
我想发送函数签名作为模板参数.这可能吗?我不能使用可变参数模板,因为我的编译器还不支持此功能.
R. *_*des 17
使用可变参数模板,您可以进行一次部分特化,将签名分解为其部分:
template<typename Signature>
class Test;
// or the SFINAE-friendlier
//template<typename Signature>
//class Test {};
// or the hard-error-friendlier
//template<typename Signature>
//class Test {
// static_assert(Bool<false, Signature>{},
// "template argument must be a signature returning void");
// // Bool is from http://flamingdangerzone.com/cxx11/2012/05/29/type-traits-galore.html#dependent_boolean
//};
template<typename... Args>
class Test<void(Args...)>
{
public:
void operator()(Args...) const;
};
Run Code Online (Sandbox Code Playgroud)
如果没有可变参数模板,则必须为每个参数数量进行一次专门化.宏可能有助于生成所有这些(Boost.PP,或者可能是Visual Studio用来模拟标准库中的可变参数模板的那些).
template <class Ty>
class Test; /* not defined */
template <class Ret, class Arg0>
class Test<Ret(Arg0)> { /* whatever */ }
template <class Ret, class Arg0, class Arg1>
class Test<Ret(Arg0, Arg1)> { /* whatever */ }
template <class Ret, class Arg0, class Arg1, class Arg2>
class Test<Ret(Arg0, Arg1, Arg2)> { /* whatever */ }
Run Code Online (Sandbox Code Playgroud)
继续繁琐的重复,直到你有足够的论据来满足你的需求.在TR1中,建议各种函数对象模板能够处理10个参数.这通常用相当复杂的宏来实现,以简化编码,但它可以通过强力来完成.