If possible, put function definitions in the class template definition. This is a template, so if you are not using the Comeau compiler, it is not as if they will be disabled in another TU.
If functions use what is defined between the class definition and the function definition, then you can play tricks to make this thing dependent on the template parameter, even when it really isnβt. For example:
template <typename T> struct Foo { void usebar(); }; struct Bar { int a; Foo<int> circularity;
becomes:
// we only have to write "same" once template <typename T, typename U> struct same { typedef U type; }; struct Bar; template <typename T> struct Foo { void usebar() { typename same<T,Bar>::type b; std::cout << ba << "\n"; } }; struct Bar { int a; Foo<int> circularity; // circularity gone Bar() : a(3) {} };
Or actually in this case, simply:
struct Bar; template <typename T, typename B = Bar> struct Foo { void usebar() { B b; std::cout << ba << "\n"; } }; struct Bar { int a; Foo<int> circularity; Bar() : a(3) {} };
All cases support the following code:
int main() { Foo<int> f; f.usebar(); }
Steve jessop
source share