Reputation: 12985
This question builds up on my:
C++ library: .hpp + .inl (separate definitions and declarations) vs .hpp only (in-class body code)
Moving templated functions into inline files is definitely an official hell. See below three examples. From my OLD neat way of doing things (#0), to a really expanded way (#1), to a more compacted way (#2) to the imaginary dream pipe way (#3).
// #0 the actual class definition
namespace CA {
template <typename tnChar>
class MyClass {
public:
typedef tnChar tChar;
typedef std::basic_string<tChar> tString;
MyClass(); // {}
tString Method1(const tString& aParam) const; // { return aParam; }
};
};
This is the inline version #1. It has both namespace and class.
template <typename tnChar>
CA::MyClass<tnChar>::MyClass(){}
template <typename tnChar>
typename CA::MyClass<tnChar>::tString
CA::MyClass<tnChar>::Method1(const tString& aParam) const {
return aParam;
}
template <> // specializing
typename CA::MyClass<wchar_t>::tString
CA::MyClass<wchar_t>::Method1(const tString& aParam) const {
return aParam;
}
This is the inline version #2. It is wrapped in namespace. Saved from adding the CA::
.
namespace CA {
template <typename tnChar>
MyClass<tnChar>::MyClass(){}
template <typename tnChar>
typename MyClass<tnChar>::tString
MyClass<tnChar>::Method1(const tString& aParam) const {
return aParam;
}
template <> // specializing
typename MyClass<wchar_t>::tString
MyClass<wchar_t>::Method1(const tString& aParam) const {
return aParam;
}
}
This is the inline version #3. It is wrapped in namespace and class. Saved from adding the template <...>
... CA::MyClass
. IMAGINARY version. Not possible but desirable!
#if 0 // like this is ever gonna work
// inline version #3 (IMAGINARY)
// wrapped in namespace and in class
// 'inline class' should make this functionality happen in C++my
namespace CA {
template <typename tnChar>
inline class MyClass { // :)
MyClass(){}
tString Method1(const tString& aParam) const {
return aParam;
}
};
template <>
inline class MyClass<wchar_t> { // :) - too much imagination
tString Method1(const tString& aParam) const {
return aParam;
}
};
}
#endif // disabled
My question is: Is anything like version 3 even remotely possible? Without having to typename
the return values and write the template <...>
each and every single time. I know the reasons why we need to do this, but I'm wondering if the new C++11
or the next C++14
has any plans to address template class inline method grouping?
Having the inline class
keywords trigger the imaginary functionality in #3 would be mind blowing and it would make externalizing template class methods so easy... and logic... and grouped... and short :)
Upvotes: 1
Views: 200
Reputation: 42554
At the risk of stating the obvious, that syntax is available when declaring member functions inside the class body. If your religion requires you to put the member functions in a separate .inl
file, you could include that file inside the class body itself.
Upvotes: 1