Splitting templated C++ classes into .hpp/.cpp files--is it possible?

前端 未结 16 854
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 17:27

I am getting errors trying to compile a C++ template class which is split between a .hpp and .cpp file:

$ g++ -c -o main.o main.cpp         


        
相关标签:
16条回答
  • 2020-11-22 17:51

    I am working with Visual studio 2010, if you would like to split your files to .h and .cpp, include your cpp header at the end of the .h file

    0 讨论(0)
  • 2020-11-22 17:52

    The place where you might want to do this is when you create a library and header combination, and hide the implementation to the user. Therefore, the suggested approach is to use explicit instantiation, because you know what your software is expected to deliver, and you can hide the implementations.

    Some useful information is here: https://docs.microsoft.com/en-us/cpp/cpp/explicit-instantiation?view=vs-2019

    For your same example: Stack.hpp

    template <class T>
    class Stack {
    
    public:
        Stack();
        ~Stack();
        void Push(T val);
        T Pop();
    private:
        T val;
    };
    
    
    template class Stack<int>;
    

    stack.cpp

    #include <iostream>
    #include "Stack.hpp"
    using namespace std;
    
    template<class T>
    void Stack<T>::Push(T val) {
        cout << "Pushing Value " << endl;
        this->val = val;
    }
    
    template<class T>
    T Stack<T>::Pop() {
        cout << "Popping Value " << endl;
        return this->val;
    }
    
    template <class T> Stack<T>::Stack() {
        cout << "Construct Stack " << this << endl;
    }
    
    template <class T> Stack<T>::~Stack() {
        cout << "Destruct Stack " << this << endl;
    }
    

    main.cpp

    #include <iostream>
    using namespace std;
    
    #include "Stack.hpp"
    
    int main() {
        Stack<int> s;
        s.Push(10);
        cout << s.Pop() << endl;
        return 0;
    }
    

    Output:

    > Construct Stack 000000AAC012F8B4
    > Pushing Value
    > Popping Value
    > 10
    > Destruct Stack 000000AAC012F8B4
    

    I however don't entirely like this approach, because this allows the application to shoot itself in the foot, by passing incorrect datatypes to the templated class. For instance, in the main function, you can pass other types that can be implicitly converted to int like s.Push(1.2); and that is just bad in my opinion.

    0 讨论(0)
  • 2020-11-22 17:54

    Only if you #include "stack.cpp at the end of stack.hpp. I'd only recommend this approach if the implementation is relatively large, and if you rename the .cpp file to another extension, as to differentiate it from regular code.

    0 讨论(0)
  • 2020-11-22 17:57

    You need to have everything in the hpp file. The problem is that the classes aren't actually created until the compiler sees that they're needed by some OTHER cpp file - so it has to have all the code available to compile the templated class at that time.

    One thing that I tend to do is to try to split my templates into a generic non-templated part (which can be split between cpp/hpp) and the type-specific template part which inherits the non-templated class.

    0 讨论(0)
提交回复
热议问题