c++ - 可变参数模板 - 有没有办法避免重复

标签 c++ variadic-templates

在代码中(只需粘贴和复制)是否有一种方法可以避免重复/列出模板参数(代码中标记的行):

#include <iostream>

using namespace std;


template<class T,class... V>
struct nullptr_
{
    nullptr_(T& obj,V&... args)
    {
        nullptr_hlp(obj,args...);
    }

    template<class A>
    static void nullptr_hlp(A& a);
    {
        a = nullptr;
    }

    template<class A,class... Vs>
    static void nullptr_hlp(A& a,Vs&... args)
    {
        a = nullptr;
        nullptr_hlp(args...);
    }

};


class X : nullptr_<int*,double*,char*>//IS THERE A WAY TO HAVE JUST nullptr_?
{

    int* a;
    double* b;
    char* c;
    typedef nullptr_<decltype(a),decltype(b),decltype(c)> init_;
public:
    X():init_(a,b,c)
    {

    }

};
int main()
{
   X x;
    return 0;
}

最佳答案

nullptr_<int*,double*,char*>成为 X 中的注入(inject)类名,因此您可以在没有参数列表的情况下引用它:

class X : nullptr_<int*,double*,char*>//can't do away with the list here, unless you want to typedef it
{

    int* a;
    double* b;
    char* c;
    //typedef nullptr_<decltype(a),decltype(b),decltype(c)> init_; //don't really need this
public:
    X():nullptr_(a,b,c) //can be used without the argument list
    {

    }
};

关于c++ - 可变参数模板 - 有没有办法避免重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8083915/

相关文章:

c++ - 任务计划程序错误消息 : 80041318, 这是什么意思?

c++ - 可变参数的冲突类型

c++ - 创建一个类模板,该模板可以将由其自身创建的类作为参数

c++ - 让指针回到第一行 - QFile

c++ - 对指针感到困惑

c++ - 在其他类中实现类似 std::array 的构造函数

c++ - 如何遍历可变模板参数以创建可变数量的局部变量?

C++ 函数 : Variadic templates with no arguments

c++ - DirectX C++ 的链接器错误

c++ - 内存映射文件很慢