具有多个可变参数模板的模板专业化 [英] Template specialization with multiple variadic templates

查看:61
本文介绍了具有多个可变参数模板的模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的最后一个问题中,我在使模板专业化工作.现在我需要一点扩展.我想要这些语句有两个专业化:

In my last question I received great help in getting a template specialization to work. Now I need a little extension. I want two specializations for these statements:

int main()
{
    // First specialization
    holder_ext<person> h1;
    holder_ext<person, &person::age> h2;
    holder_ext<int> h3;

    // Second specialization
    holder_ext<person, &person::age, &person::name> h4;
}

我班上的人看起来像这样:

My class person looks like this:

class person
{
private:
    std::string name_;
    int age_;
public:
    person(const std::string &name)
        : name_(name), age_(56)
    {}
    void age(int a) { age_ = i; }
    void name(const std::string &n) { name_ = n; }
};

特殊的是,两个成员函数具有不同的参数类型.因此,我不能为两者使用相同的可变参数模板成员函数.我尝试了两种不同的可变参数模板.但这是行不通的.成员函数的默认值也不起作用.

The special thing is, that the two member functions have different parameter types. So I can't use the same variadic template member function for both. I tried it with two different variadic templates. But that doesn't work. Also default values for the member functions do not work.

有人对我有很好的提示吗?

Does anybody have a good hint for me?

这是具有一个成员函数的解决方案(感谢 Pubby ):

This is the solution with one member function (thanks to Pubby):

template < class T, void (std::conditional<std::is_class<T>::value, T, struct dummy>::type::* ...FUNC)(int)> class holder;

template < class T, void (T::*FUNC)(int)>
class holder<T, FUNC>
{
public:
    explicit holder() : setter(FUNC) { std::cout << "func\n"; }
private:
    std::function<void (value_type&, int)> setter;
};

template < class T>
class holder<T>
{
public:
    explicit holder() { std::cout << "plain\n"; }
};

再次感谢!

P.S .:不,我不会在两天内提出三个,四个,五个成员函数必须做什么"吗?;-)

P.S.: And no, I won't come up in two days with "what must do with three, four, five member functions"? ;-)

推荐答案

最后,我找到了解决问题的方法.它是可变参数模板和模板规范化的混合:

Finally I found a solution for my problem. It is a mix between variadic templates and template specilization:

template < class T,
void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC1)(int) = nullptr,
void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::* ...FUNC2)(const std::string&)
>
class holder_ext;

template < class T,
void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC1)(int),
void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC2)(const std::string&)
>
class holder_ext<T, FUNC1, FUNC2>
{
public:
    holder_ext() { std::cout << "func 2 test\n"; }
};

template < class T,
void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC1)(int)
>
class holder_ext<T, FUNC1>
{
public:
    holder_ext() { std::cout << "func 1 test\n"; }
};

我使用了一个未实现的声明,并定义了两个特化.一个具有成员功能,另一个具有其他所有功能.

I use a not implemented declaration and define two specializations. One with both member function and the other one for all other cases.

如果有更好的解决方案,请随时告诉我.

If there is a better solution dont't hesitate to tell me.

这篇关于具有多个可变参数模板的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆