I've got a template class B with an inner class A. Class A has a member function f that I want to return a reference to itself (i.e., a reference to an object of type A). I keep getting inexplicable compiler errors.
Here's what I know:
If I change the return type to void it compiles.
If I get rid of the outer class, it compiles.
If I get rid of the templates, it compiles.
If I move the function implementation into the class declaration, it compiles. (This technique actually gets me what I want, I just like keeping my method implementations out of the class declaration as a matter of style.)
Here are two versions of the classes in one file -- the first I changed the return type to void just to show the problem is with the return type.
inlinetypename B2<T>::A2& B2<T>::A2::f(const A2& a) { solved it. When you do :: while using templates C++ has problems recognizing what the thing after :: is. Maybe this makes sense because due to template specialization Foo<int>::Bar could be a type while Foo<char>::Bar was a static variable..
By the way, these are the errors MSVC++ found:
main.cpp(12): warning C4346: 'B2<T>::A2' : dependent name is not a type
prefix with 'typename' to indicate a type
main.cpp(12): error C2143: syntax error : missing ';' before '&'
main.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
gnu clearly needs to improve their warning/error messaging. What a nightmare. The sad thing is I now remember reading about this usage for typename a few years back; but since I've never previously had the need for it, I guess it didn't really sink in.