C++ 使用c++;自动生成重复代码的预处理器

C++ 使用c++;自动生成重复代码的预处理器,c++,for-loop,preprocessor,variadic-macros,C++,For Loop,Preprocessor,Variadic Macros,我目前正在处理一段极其重复的代码,它正在实现和实例化一系列模板构造函数。一般模式有点像这样: // preamble: some dummy classes to work with class A{}; class B{}; class C{}; class D{}; class E{}; class F{}; class G{}; class X{}; class Y{}; // the actual class declaration, located in some header f

我目前正在处理一段极其重复的代码,它正在实现和实例化一系列模板构造函数。一般模式有点像这样:

// preamble: some dummy classes to work with
class A{};
class B{};
class C{};
class D{};
class E{};
class F{};
class G{};

class X{};
class Y{};

// the actual class declaration, located in some header file

class MyClass {
  A* _a;
  B* _b;
  C* _c;
  D* _d;
  E* _e;
  F* _f;
  G* _g;

 public:
  template<class T> void foo(const std::vector<T>& args){};

  MyClass(A* a = NULL, B* b = NULL, C* c = NULL, D* d = NULL, E* e = NULL, F* f = NULL, G* g = NULL);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, std::vector<T> args1);
  template<class T> MyClass(A* a, std::vector<T> args1);
  template<class T> MyClass(std::vector<T> args1);
};

// default constructor, this is still fine 

MyClass::MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g) : _a(a),_b(b),_c(c),_d(c),_e(e),_f(f),_g(g) {};

// here the horribly repetitive part begins
// there are lots of repetitions of this block, every time with a slightly different signature

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1) : MyClass(a,b,c,d,e,f,g) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, F*, G*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, F*, G*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, std::vector<T> args1) : MyClass(a,b,c,d,e,f) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, F*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, F*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, std::vector<T> args1) : MyClass(a,b,c,d,e) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, std::vector<T> args1) : MyClass(a,b,c,d) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, std::vector<T> args1) : MyClass(a,b,c) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, std::vector<T> args1) : MyClass(a,b) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, std::vector<X> args1);
template MyClass<Y>(A*, B*, std::vector<Y> args1);

template<class T> MyClass(A* a, std::vector<T> args1) : MyClass(a) {
  foo<T>(args);
}
template MyClass<X>(A*, std::vector<X> args1);
template MyClass<Y>(A*, std::vector<Y> args1);

template<class T> MyClass(A* a, std::vector<T> args1) : MyClass() {
  foo<T>(args);
}
template MyClass<X>(std::vector<X> args1);
template MyClass<Y>(std::vector<Y> args1);

// here some main function just to make it compile    

int main(){
  std::vector<X> x;
  MyClass c(NULL,NULL,NULL,x);
  return 1;
}
#include <stdio.h>
#include <vector>

#define PARAMS_1 A* a,                                     std::vector<T> args1
#define PARAMS_2 A* a, B* b,                               std::vector<T> args1
#define PARAMS_3 A* a, B* b, C* c,                         std::vector<T> args1
#define PARAMS_4 A* a, B* b, C* c, D* d,                   std::vector<T> args1
#define PARAMS_5 A* a, B* b, C* c, D* d, E* e,             std::vector<T> args1
#define PARAMS_6 A* a, B* b, C* c, D* d, E* e, F* f,       std::vector<T> args1
#define PARAMS_7 A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1

#define ARGS_1 a
#define ARGS_2 a,b
#define ARGS_3 a,b,c
#define ARGS_4 a,b,c,d
#define ARGS_5 a,b,c,d,e
#define ARGS_6 a,b,c,d,e,f
#define ARGS_7 a,b,c,d,e,f,g

#define DECLAREHEADER(x) template<class T> MyClass(PARAMS_##x)

// preamble: some dummy classes to work with
class A{};
class B{};
class C{};
class D{};
class E{};
class F{};
class G{};

class X{};
class Y{};

// the actual class declaration, located in some header file
class MyClass {
  A* _a;
  B* _b;
  C* _c;
  D* _d;
  E* _e;
  F* _f;
  G* _g;

 public:
  template<class T> void foo(const std::vector<T>& args){};

  MyClass(A* a = NULL, B* b = NULL, C* c = NULL, D* d = NULL, E* e = NULL, F* f = NULL, G* g = NULL);

  DECLAREHEADER(7);
  DECLAREHEADER(6);
  DECLAREHEADER(5);
  DECLAREHEADER(4);
  DECLAREHEADER(3);
  DECLAREHEADER(2);
  DECLAREHEADER(1);
};

// default constructor, this is still fine
MyClass::MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g) : _a(a),_b(b),_c(c),_d(d),_e(e),_f(f),_g(g) {}

#define DECLAREALL(x) template<class T> MyClass :: MyClass(PARAMS_##x) : MyClass(ARGS_##x) { foo<T>(args1); }

DECLAREALL(7);
DECLAREALL(6);
DECLAREALL(5);
DECLAREALL(4);
DECLAREALL(3);
DECLAREALL(2);
DECLAREALL(1);

int main(){
  std::vector<X> x;
  MyClass c(NULL,NULL,NULL,x);
  return 1;
}
我可以想象,可以将
生成_构造函数
编写为带有
FOREACH
循环的可变预处理器宏,但不幸的是,我能找到的关于可变预处理器宏主题的所有文档对我来说都非常混乱,而且没有太大的帮助,因为大多数示例都是围绕一些技术来包装
printf


有什么好方法可以提取出这样重复的代码段,可能是使用预处理器的魔法?

我的技能不足以尝试可变宏,但您可以通过常规的旧非可变宏删除大量重复代码,如下所示:

// preamble: some dummy classes to work with
class A{};
class B{};
class C{};
class D{};
class E{};
class F{};
class G{};

class X{};
class Y{};

// the actual class declaration, located in some header file

class MyClass {
  A* _a;
  B* _b;
  C* _c;
  D* _d;
  E* _e;
  F* _f;
  G* _g;

 public:
  template<class T> void foo(const std::vector<T>& args){};

  MyClass(A* a = NULL, B* b = NULL, C* c = NULL, D* d = NULL, E* e = NULL, F* f = NULL, G* g = NULL);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, std::vector<T> args1);
  template<class T> MyClass(A* a, std::vector<T> args1);
  template<class T> MyClass(std::vector<T> args1);
};

// default constructor, this is still fine 

MyClass::MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g) : _a(a),_b(b),_c(c),_d(c),_e(e),_f(f),_g(g) {};

// here the horribly repetitive part begins
// there are lots of repetitions of this block, every time with a slightly different signature

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1) : MyClass(a,b,c,d,e,f,g) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, F*, G*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, F*, G*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, std::vector<T> args1) : MyClass(a,b,c,d,e,f) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, F*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, F*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, std::vector<T> args1) : MyClass(a,b,c,d,e) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, std::vector<T> args1) : MyClass(a,b,c,d) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, std::vector<T> args1) : MyClass(a,b,c) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, std::vector<T> args1) : MyClass(a,b) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, std::vector<X> args1);
template MyClass<Y>(A*, B*, std::vector<Y> args1);

template<class T> MyClass(A* a, std::vector<T> args1) : MyClass(a) {
  foo<T>(args);
}
template MyClass<X>(A*, std::vector<X> args1);
template MyClass<Y>(A*, std::vector<Y> args1);

template<class T> MyClass(A* a, std::vector<T> args1) : MyClass() {
  foo<T>(args);
}
template MyClass<X>(std::vector<X> args1);
template MyClass<Y>(std::vector<Y> args1);

// here some main function just to make it compile    

int main(){
  std::vector<X> x;
  MyClass c(NULL,NULL,NULL,x);
  return 1;
}
#include <stdio.h>
#include <vector>

#define PARAMS_1 A* a,                                     std::vector<T> args1
#define PARAMS_2 A* a, B* b,                               std::vector<T> args1
#define PARAMS_3 A* a, B* b, C* c,                         std::vector<T> args1
#define PARAMS_4 A* a, B* b, C* c, D* d,                   std::vector<T> args1
#define PARAMS_5 A* a, B* b, C* c, D* d, E* e,             std::vector<T> args1
#define PARAMS_6 A* a, B* b, C* c, D* d, E* e, F* f,       std::vector<T> args1
#define PARAMS_7 A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1

#define ARGS_1 a
#define ARGS_2 a,b
#define ARGS_3 a,b,c
#define ARGS_4 a,b,c,d
#define ARGS_5 a,b,c,d,e
#define ARGS_6 a,b,c,d,e,f
#define ARGS_7 a,b,c,d,e,f,g

#define DECLAREHEADER(x) template<class T> MyClass(PARAMS_##x)

// preamble: some dummy classes to work with
class A{};
class B{};
class C{};
class D{};
class E{};
class F{};
class G{};

class X{};
class Y{};

// the actual class declaration, located in some header file
class MyClass {
  A* _a;
  B* _b;
  C* _c;
  D* _d;
  E* _e;
  F* _f;
  G* _g;

 public:
  template<class T> void foo(const std::vector<T>& args){};

  MyClass(A* a = NULL, B* b = NULL, C* c = NULL, D* d = NULL, E* e = NULL, F* f = NULL, G* g = NULL);

  DECLAREHEADER(7);
  DECLAREHEADER(6);
  DECLAREHEADER(5);
  DECLAREHEADER(4);
  DECLAREHEADER(3);
  DECLAREHEADER(2);
  DECLAREHEADER(1);
};

// default constructor, this is still fine
MyClass::MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g) : _a(a),_b(b),_c(c),_d(d),_e(e),_f(f),_g(g) {}

#define DECLAREALL(x) template<class T> MyClass :: MyClass(PARAMS_##x) : MyClass(ARGS_##x) { foo<T>(args1); }

DECLAREALL(7);
DECLAREALL(6);
DECLAREALL(5);
DECLAREALL(4);
DECLAREALL(3);
DECLAREALL(2);
DECLAREALL(1);

int main(){
  std::vector<X> x;
  MyClass c(NULL,NULL,NULL,x);
  return 1;
}
#包括
#包括
#定义参数A*A,std::vector args1
#定义参数2 A*A,B*B,std::vector args1
#定义参数3 A*A,B*B,C*C,std::vector args1
#定义参数4 A*A,B*B,C*C,D*D,std::vector args1
#定义参数A*A,B*B,C*C,D*D,E*E,std::vector args1
#定义参数A*A,B*B,C*C,D*D,E*E,F*F,std::vector args1
#定义参数A*A,B*B,C*C,D*D,E*E,F*F,G*G,std::vector args1
#定义ARGS_1 a
#定义ARGS_2 a,b
#定义ARGS_3 a、b、c
#定义ARGS_4 a、b、c、d
#定义ARGS_5 a、b、c、d、e
#定义ARGS_6 a、b、c、d、e、f
#定义参数7 a、b、c、d、e、f、g
#定义DECLAREHEADER(x)模板MyClass(参数x)
//序言:一些要使用的虚拟类
A类{};
B类{};
C类{};
D类{};
E类{};
类别F{};
G类{};
类X{};
类Y{};
//实际的类声明,位于某些头文件中
类MyClass{
A*_A;
B*_B;
C*(u C);
D*(D);;
E*(E);
F*\u F;
G*_G;
公众:
模板void foo(const std::vector&args){};
MyClass(A*A=NULL,B*B=NULL,C*C=NULL,D*D=NULL,E*E=NULL,F*F=NULL,G*G=NULL);
申报人(7);
申报人(6);
申报人(5);
申报人(4);
申报人(3);
申报人(2);
申报人(1);
};
//默认构造函数,这仍然可以
MyClass::MyClass(A*A,B*B,C*C,D*D,E*E,F*F,G*G):\u A(A),\u B(B),\u C(C),\u D(D),\u E(E),\u F(F),\u G(G){}
#定义DECLAREALL(x)模板MyClass::MyClass(参数x):MyClass(参数x){foo(args1)}
十二月份(7);
公告(6);
声明(5);
声明(4);
声明(3);
声明(2);
12月1日;
int main(){
std::向量x;
MyClass c(NULL,NULL,NULL,x);
返回1;
}

请注意,我省略了
模板MyClass
模板MyClass
行,因为我不确定它们的目的是什么,但您应该能够很容易地将它们添加回
DECLAREALL
宏的定义中。

我的技能不足以尝试可变宏,但您可以通过常规的旧非可变宏删除大量重复代码,如下所示:

// preamble: some dummy classes to work with
class A{};
class B{};
class C{};
class D{};
class E{};
class F{};
class G{};

class X{};
class Y{};

// the actual class declaration, located in some header file

class MyClass {
  A* _a;
  B* _b;
  C* _c;
  D* _d;
  E* _e;
  F* _f;
  G* _g;

 public:
  template<class T> void foo(const std::vector<T>& args){};

  MyClass(A* a = NULL, B* b = NULL, C* c = NULL, D* d = NULL, E* e = NULL, F* f = NULL, G* g = NULL);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, D* d, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, C* c, std::vector<T> args1);
  template<class T> MyClass(A* a, B* b, std::vector<T> args1);
  template<class T> MyClass(A* a, std::vector<T> args1);
  template<class T> MyClass(std::vector<T> args1);
};

// default constructor, this is still fine 

MyClass::MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g) : _a(a),_b(b),_c(c),_d(c),_e(e),_f(f),_g(g) {};

// here the horribly repetitive part begins
// there are lots of repetitions of this block, every time with a slightly different signature

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1) : MyClass(a,b,c,d,e,f,g) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, F*, G*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, F*, G*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, F* f, std::vector<T> args1) : MyClass(a,b,c,d,e,f) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, F*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, F*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, E* e, std::vector<T> args1) : MyClass(a,b,c,d,e) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, E*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, E*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, D* d, std::vector<T> args1) : MyClass(a,b,c,d) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, D*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, D*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, C* c, std::vector<T> args1) : MyClass(a,b,c) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, C*, std::vector<X> args1);
template MyClass<Y>(A*, B*, C*, std::vector<Y> args1);

template<class T> MyClass(A* a, B* b, std::vector<T> args1) : MyClass(a,b) {
  foo<T>(args);
}
template MyClass<X>(A*, B*, std::vector<X> args1);
template MyClass<Y>(A*, B*, std::vector<Y> args1);

template<class T> MyClass(A* a, std::vector<T> args1) : MyClass(a) {
  foo<T>(args);
}
template MyClass<X>(A*, std::vector<X> args1);
template MyClass<Y>(A*, std::vector<Y> args1);

template<class T> MyClass(A* a, std::vector<T> args1) : MyClass() {
  foo<T>(args);
}
template MyClass<X>(std::vector<X> args1);
template MyClass<Y>(std::vector<Y> args1);

// here some main function just to make it compile    

int main(){
  std::vector<X> x;
  MyClass c(NULL,NULL,NULL,x);
  return 1;
}
#include <stdio.h>
#include <vector>

#define PARAMS_1 A* a,                                     std::vector<T> args1
#define PARAMS_2 A* a, B* b,                               std::vector<T> args1
#define PARAMS_3 A* a, B* b, C* c,                         std::vector<T> args1
#define PARAMS_4 A* a, B* b, C* c, D* d,                   std::vector<T> args1
#define PARAMS_5 A* a, B* b, C* c, D* d, E* e,             std::vector<T> args1
#define PARAMS_6 A* a, B* b, C* c, D* d, E* e, F* f,       std::vector<T> args1
#define PARAMS_7 A* a, B* b, C* c, D* d, E* e, F* f, G* g, std::vector<T> args1

#define ARGS_1 a
#define ARGS_2 a,b
#define ARGS_3 a,b,c
#define ARGS_4 a,b,c,d
#define ARGS_5 a,b,c,d,e
#define ARGS_6 a,b,c,d,e,f
#define ARGS_7 a,b,c,d,e,f,g

#define DECLAREHEADER(x) template<class T> MyClass(PARAMS_##x)

// preamble: some dummy classes to work with
class A{};
class B{};
class C{};
class D{};
class E{};
class F{};
class G{};

class X{};
class Y{};

// the actual class declaration, located in some header file
class MyClass {
  A* _a;
  B* _b;
  C* _c;
  D* _d;
  E* _e;
  F* _f;
  G* _g;

 public:
  template<class T> void foo(const std::vector<T>& args){};

  MyClass(A* a = NULL, B* b = NULL, C* c = NULL, D* d = NULL, E* e = NULL, F* f = NULL, G* g = NULL);

  DECLAREHEADER(7);
  DECLAREHEADER(6);
  DECLAREHEADER(5);
  DECLAREHEADER(4);
  DECLAREHEADER(3);
  DECLAREHEADER(2);
  DECLAREHEADER(1);
};

// default constructor, this is still fine
MyClass::MyClass(A* a, B* b, C* c, D* d, E* e, F* f, G* g) : _a(a),_b(b),_c(c),_d(d),_e(e),_f(f),_g(g) {}

#define DECLAREALL(x) template<class T> MyClass :: MyClass(PARAMS_##x) : MyClass(ARGS_##x) { foo<T>(args1); }

DECLAREALL(7);
DECLAREALL(6);
DECLAREALL(5);
DECLAREALL(4);
DECLAREALL(3);
DECLAREALL(2);
DECLAREALL(1);

int main(){
  std::vector<X> x;
  MyClass c(NULL,NULL,NULL,x);
  return 1;
}
#包括
#包括
#定义参数A*A,std::vector args1
#定义参数2 A*A,B*B,std::vector args1
#定义参数3 A*A,B*B,C*C,std::vector args1
#定义参数4 A*A,B*B,C*C,D*D,std::vector args1
#定义参数A*A,B*B,C*C,D*D,E*E,std::vector args1
#定义参数A*A,B*B,C*C,D*D,E*E,F*F,std::vector args1
#定义参数A*A,B*B,C*C,D*D,E*E,F*F,G*G,std::vector args1
#定义ARGS_1 a
#定义ARGS_2 a,b
#定义ARGS_3 a、b、c
#定义ARGS_4 a、b、c、d
#定义ARGS_5 a、b、c、d、e
#定义ARGS_6 a、b、c、d、e、f
#定义参数7 a、b、c、d、e、f、g
#定义DECLAREHEADER(x)模板MyClass(参数x)
//序言:一些要使用的虚拟类
A类{};
B类{};
C类{};
D类{};
E类{};
类别F{};
G类{};
类X{};
类Y{};
//实际的类声明,位于某些头文件中
类MyClass{
A*_A;
B*_B;
C*(u C);
D*(D);;
E*(E);
F*\u F;
G*_G;
公众:
模板void foo(const std::vector&args){};
MyClass(A*A=NULL,B*B=NULL,C*C=NULL,D*D=NULL,E*E=NULL,F*F=NULL,G*G=NULL);
申报人(7);
申报人(6);
申报人(5);
申报人(4);
申报人(3);
申报人(2);
申报人(1);
};
//默认构造函数,这仍然可以
MyClass::MyClass(A*A,B*B,C*C,D*D,E*E,F*F,G*G):\u A(A),\u B(B),\u C(C),\u D(D),\u E(E),\u F(F),\u G(G){}
#定义DECLAREALL(x)模板MyClass::MyClass(参数x):MyClass(参数x){foo(args1)}
十二月份(7);
公告(6);
声明(5);
声明(4);
声明(3);
声明(2);
12月1日;
int main(){
std::向量x;
MyClass c(NULL,NULL,NULL,x);
返回1;
}

注意,我省略了
template MyClass
template MyClass
行,因为我不确定它们的意图是什么,但是您应该能够很容易地将它们添加回
DECLAREALL
宏的定义中。

boost
具有某种预处理器
FOREACH
宏助手。旁白:为什么不使用一个构造函数
模板MyClass(std::vector args={},A*A=NULL,B*B=NULL,C*C=NULL,D*D=NULL,E*E=NULL,F*F=NULL,G*G=NULL)?@Caleth:实际上这是个好建议。在当前的特定场景中,这不是一个选项,因为
A
B
C
等部分相互继承,因此在某些情况下,需要最终对象来解决函数调用方式中令人困惑的歧义,但无论如何,这不是一个很好的设计,所以谢谢你的建议
boost
具有某种预处理器
FOREACH
宏助手。旁白:为什么不使用一个构造函数
模板MyClass(std::vector args={},A*A=NULL,B*