0

I came across a head scratcher in my coding endeavors, and was wondering if anyone could give me some insight on it. I have a header only class, that I was going to split into .h/.cpp, and while writing out the .cpp file, I noticed that I had no idea how to write methods for a nested class in a class template. Here's a code snippet (small part of what already runs):

namespace core { 
    namespace container {
        template <class T>
        class List { // the class 
        protected:
            class Node {
            public:
                T data;
                Node* next;
                inline Node(T d) : data(d),next(NULL) {}
                inline Node(T d,Node* n) : data(d),next(n) {}
                inline ~Node() {next = NULL;}
            };
        public:
            // the rest of the class template, a here
        };
    };
};

The snippet is highly stripped down, with relevant stuff left behind. Normally, I don't deal with this as I tend to keep all my template stuff header-only, but compiling needs in other parts of the project would be smoother if I compile all this and hang on to the objects.

So initially I wrote

using namespace core::container;
template <class T>
List<T>::Node::Node(T d) {}

Now this isn't exactly the line, because I honestly don't intend on removing the inline constructor anyway, but I used it as an example because it's at the top of the code and simple.

I'm not sure if it was me or just that my IDE red-lined it, but it looked wrong to me. I'm away from my compiling machine so I can't run a test on it, and was wondering if I could get some insight before I write a few hundred incorrect lines.

So, am I doing this wrong? What is the proper way of defining a member function (or constructor in this example) for a subclass of a class template?

Werlious
  • 583
  • 6
  • 15
  • Side note: Setting members to `NULL` is meaningless in destructors, the pointer is destroyed afterwards anyway. Apart from, you should prefer C++ *keywords* (`nullptr`) over old (obsolete?) C *macros* (`NULL`) – unless you explicitly target pre-C++11 compilers. – Aconcagua Jun 12 '20 at 05:39
  • @Aconcagua It's old dusty code I've revived from my graveyard folder, I do agree it needs to be updated. And no, target is c++14. The code works so I'm honestly not worried too much about it, I just would like to figure out how to pull out the implementation on it first, then I can bring it up do date – Werlious Jun 12 '20 at 06:03
  • Never mind, just a recommendation. But as the duplicate explains, it is not possible to export templates' implementations into separate .cpp files. Well – you *can* separate definition and implementation. The compiler is fine with seeing `template class C { void f(); }; template void C::f() { }`, but it requires the full implementation available *always*. – Aconcagua Jun 12 '20 at 06:41
  • So if you really create a separate .cpp file, you would have to include it: `template class C { void f(); }; #include `. I is not recommended to use file extension .cpp in that case, though. Many libraries that do such separation use .inl instead, in the duplicate .tpp is proposed in one answer. – Aconcagua Jun 12 '20 at 06:41

0 Answers0