В настоящее время я имею дело с частью чрезвычайно повторяющегося кода, который реализует и создает серию конструкторов шаблонов. Общая схема выглядит примерно так:
// 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;
}
Как видите, существует определенный тип блока, который повторяется снова и снова
template<class T> MyClass(ARG* arg, ..., const std::vector<T> &args) : Myclass(arg,...) { foo<T>(args); }
template MyClass<X>(ARG* arg, ..., const std::vector<X>& args)
template MyClass<Y>(ARG* arg, ..., const std::vector<Y>& args)
Этот код, конечно, является кошмаром, и я постоянно использую sed
для редактирования кода, что, конечно, является плохой практикой и в какой-то момент обязательно приведет к сбою.
Я думаю, что повторение можно абстрагировать с помощью некоторого макроса препроцессора
#define GENERATE_CONSTRUCTOR(...) ???
GENERATE_CONSTRUCTOR(A,a,B,b,C,c,D,d)
Я бы предположил, что можно было бы написать GENERATE_CONSTRUCTOR
в виде некоторого макроса вариадического препроцессора с циклом FOREACH
, но, к сожалению, вся документация, которую я смог найти по теме макросов вариационного препроцессора, меня сильно смутила, и не очень полезно, так как большинство примеров сосредоточено вокруг некоторой техники для переноса printf
.
Что было бы хорошим способом абстрагировать такие повторяющиеся сегменты кода, возможно, используя магию препроцессора?