Where to define C++ class member template function and functors that instantiate it?

后端 未结 4 809
[愿得一人]
[愿得一人] 2020-12-16 04:47

I have a class Foo which is used in a small standalone project. It has a class definition in Foo.h with the implementation for the class\' member functions in an implementat

4条回答
  •  北海茫月
    2020-12-16 05:44

    My default would be to put the definition for the member function templates right in the .h file, like this:

    class Foo
    {
    public: 
      template void DoSomething(T t);
    };
    
    // ... later...
    
    template
    void Foo::DoSomething(T t)
    {
      // ...
    }
    

    If this is suboptimal for a particular case, then I'd take more heroic measures. Starting with #include-ing a .inc file with the definition at the end of the .h file, or possibly even doing explicit instantiations in the .cpp files where I needed the member function templates to be used.

提交回复
热议问题