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

后端 未结 4 812
[愿得一人]
[愿得一人] 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:32

    The template method definition should indeed be in the header file of it the class it belongs to.

    Like this:

    class MyClass
    {
        template 
        void foo(const T&)
        {
            // Definition
        }
    };
    

    Or like this (note that the template method definition can be included from separate file after the class declaration)

    class MyClass
    {
        template  void foo(const T&);
    };
    
    template 
    void MyClass::foo(const T&)
    {
        // Definition
    }
    

    The rest is depends on the style you agreed on and your needs.

    I would put the functor declaration (or even the definition if they are simple) into the header if I use them not only in Foo or if Foo has them as class member.

提交回复
热议问题