1

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 :)

Community
  • 1
  • 1
CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • Will the `auto` keyword do this? I *think* I've seen `auto` on return types, but I might be mistaken. – Suedocode Aug 05 '13 at 18:30
  • @Aggieboy `MSVC` is kind of slow in catching up with things... `auto` returns... not yet! And don't think it applies here. – CodeAngry Aug 05 '13 at 18:40
  • @Aggieboy An interesting hint, the trailing-return-type is indeed in the scope of the class. – dyp Aug 05 '13 at 18:40

1 Answers1

1

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.

Casey
  • 41,449
  • 7
  • 95
  • 125