C++ templates declare in .h, define in .hpp

后端 未结 7 1726
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 05:08

I saw some code in which the developer defined a class template in a .h file, and defined its methods in a .hpp file. This caught me a bit by surprise.

Are there are

相关标签:
7条回答
  • 2020-12-05 06:13

    Typically (in my experience, YMMV) an hpp file is an #include-ed CPP file. This is done in order to break the code up in to two physical files, a primary include and an implementation-details file that the users of your library don't need to know about. It is done like this:

    super_lib.h (the only file your clients need to #include)

    template<...> class MyGizmo
    {
    public:
      void my_fancy_function();
    };
    
    #include "super_lib_implementation.hpp"
    

    super_lib_implementation.hpp (your clients do not #include this directly)

    template<...> void MyGizmo<...>::my_fancy_function()
    {
     // magic happens
    }
    
    0 讨论(0)
提交回复
热议问题