Reputation: 10539
Consider for a moment this code. Is not important what the code do, but how template boilerplate code is written.
From boilerplate point of view, the code has some important advantages.
much less boilerplate code.
if you decide to take more template parameters, you change only class definition and the #define
.
Disadvantages are obvious:
using preprocessor
not so readable
just weird
Is there anything standardized like this?
#include <cstdio>
template<typename T>
struct Summator{
Summator(T v1, T v2);
T value1();
T value2();
T sum();
bool great();
private:
T v1;
T v2;
};
#define Summator_(type) template<typename T> type Summator<T>
Summator_()::Summator(T v1, T v2) : v1(v1), v2(v2){};
Summator_(T)::value1(){
return v1;
}
Summator_(T)::value2(){
return v1;
}
Summator_(T)::sum(){
return v1 + v2;
}
Summator_(bool)::great(){
return v1 > v2;
}
int main(){
Summator<int> s{ 5, 6 };
printf("%d\n", s.sum() );
}
Upvotes: 3
Views: 1456
Reputation: 69864
much less boilerplate code.
No, it's the same
if you decide to take more template parameters, you change only class definition and the #define.
read: if you decide to completely change the design... maybe you might save some typing (but then again, maybe not)
using preprocessor
Yup - so that's a no right there.
not so readable
so "no" again
just weird
irrelevant
Is there anything standardized like this?
clearly not
Upvotes: 0
Reputation: 17705
If I understand your question correctly you want to avoid to have to write the whole template in front of every function definition?
I think most people who care about this rely on an IDE to generate empty function definitions / refactor existing functions?
I don't think using macro's is a good idea for this, just to save a little typing, feels like a maintenance nightmare in the making.
Upvotes: 1
Reputation: 28987
Given that for most practical purposes, member functions of class templates have to be inline, you might as well define them in the body of the calls. Particularly for tiny functions like this.
Obviously real class templates are likely to have one or two non-trivial functions - but then you only need the boiler plate for those one or two functions.
There is nothing defined in the standard to help with this, and I'm not sure I'd want there to be. If you must do it, remember to #undef the define afterwards (because otherwise you will pollute the #define namespace of the user of your class).
Upvotes: 1