简体   繁体   English

方法作为可变参数模板参数

[英]Methods as variadic template arguments

I'm trying to extend to methods the technique explained here for functions. 我正在尝试扩展到这里解释函数的方法。 The problem is the method signature template parameter. 问题是方法签名模板参数。 For instance, a wrapper for the sin function is created this way: 例如,sin函数的包装器以这种方式创建:

template<typename Sig, Sig& S> struct wrapper;

template<typename Ret, typename... Args, Ret(&P)(Args...)>
struct wrapper<Ret(Args...), P> {
    // blah
}

and then is instantiated with 然后用。实例化

wrapper<decltype(sin), sin>

But for a method bool Foo::blah(int) this technique is rejected: 但是对于方法bool Foo::blah(int)这个技术被拒绝了:

template<class C, typename Sig, Sig& S> struct wrapper;

template<class C, typename Ret, typename... Args, Ret(C::*P)(Args...)>
struct wrapper<Ret(C::)(Args...), P> {
    // blah
}

wrapper<decltype(Foo::blah), &Foo::blah>

So what's the proper syntax? 那么正确的语法是什么?

There is no analogue of free function types for member functions. 成员函数没有自由函数类型的类比。 You need to work with the pointers-to-member-functions directly. 您需要直接使用指向成员函数的指针。

Try something like this: 尝试这样的事情:

template <typename C, typename MFP, MFP> struct wrapper;

template <typename C, typename R, typename ...Args, R (C::*MFP)(Args...)>
struct wrapper<C, R (C::*)(Args...), MFP>
{
    // ...
};

Note that this will get a bit verbose if you want to admit all the possible combinations of CV- and rvalue-qualifications. 请注意,如果您想要承认CV和rvalue资格的所有可能组合,这将会有点冗长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM