模板化类中的模板化成员函数的定义(C ++)

czu*_*ger 2 c++ templates

我有以下模板化类,在.hpp文件中声明,实现在.hpp文件末尾的.inl文件中。它具有模板化副本构造函数,但我不知道也找不到在.inl文件中实现模板化副本构造函数的正确语法。有谁知道正确的语法吗?

Foo.hpp的内容

template <class X>
class Foo
{
public:
    explicit Foo(Bar* bar);    

    //I would like to move the definition of this copy ctor to the .inl file
    template <class Y> explicit Foo(Foo<Y> const& other) :
       mBar(other.mBar)
    {
      assert(dynamic_cast<X>(mBar->someObject()) != NULL);
      //some more code
    }

    void someFunction() const;

private:
    Bar* mBar;
}
#include Foo.inl
Run Code Online (Sandbox Code Playgroud)

Foo.inl的内容

template <class X>
Foo<X>::Foo(Bar* bar) : 
   mBar(bar)
{
   //some code
}

template <class X>
Foo<X>::someFunction()
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

sth*_*sth 5

对于构造函数,您有两个嵌套模板,并且在.inl文件中定义它们时必须同时指定两者:

template <class X>
template <class Y>
Foo<X>::Foo(Foo<Y> const& other) : mBar(other.mBar) {
   assert(dynamic_cast<X>(mBar->someObject()) != NULL);
   //some more code
}
Run Code Online (Sandbox Code Playgroud)