开发者

Definition of templated member function in templated class (C++)

I have the following templated class, declared in an .hpp file with the implementation in a .inl file included at the end of the .hpp file. It has a templated copy constructor, but I don't know nor can't find anywhere the correct syntax for implementing the templated copy constructor in the .inl file. Does anyone know the correct syntax for this?

Contents of 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

Contents of Foo.inl

template <class X>开发者_如何学Go
Foo<X>::Foo(Bar* bar) : 
   mBar(bar)
{
   //some code
}

template <class X>
Foo<X>::someFunction()
{
    //do stuff
}


For the constructor you have two nested templates and you have to specify both when you define it in the .inl file:

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜