gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:28:01 26 4
gpt4 key购买 nike

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

#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com