小编czu*_*ger的帖子

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

我有以下模板化类,在.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)

c++ templates

2
推荐指数
1
解决办法
299
查看次数

标签 统计

c++ ×1

templates ×1