c++ - How to induce some template classes being united by one template? -
i have template classes. united 1 namespace, , depends on each other's template parameter.
that point using #define t instead of template, , use in classes, client classes may want create such pairs different t, why want use templates.
but if create 2 separated classes own separated templates, have chance client make mistake , put different values there. so, avoid it, if possible, make set t once pair of such classes , use both classes it's value.
i create (just imagine):
template<int t> namespace sample { struct { char _data[t]; } struct b { void get(a& a) { memcpy(b, a._data, t); } char b[t]; } }
so, there separated classes, if 1 has parameter t = 50, other have work same parameter. best solution - template namespace, c++ has no template namespaces.
is possible make somehow? maybe need pattern?
i don't want add like:
char x1[t1 - t2 + 1]; char x2[t2 - t1 + 1];
inside class b, error if t1 != t2 @ compilation, find simple , beauty solution task, believe have exist :-)
use nested classes. replace namespace
struct
.
template<int t> struct sample { struct { char _data[t]; }; struct b{ // ... }; // can have static methods operate on types // same template instance without specifying type static void foo(b& b) { a{0}; b.get(a); } }; int main() { sample<2>::a a{0}; sample<2>::b b; b.get(a); }
perhaps remove constructor of sample
no 1 tries instantiate it.
Comments
Post a Comment