c++11 inheriting template constructors

强颜欢笑 提交于 2019-12-10 03:14:25

问题


I find the syntax of the constructor inheritance slightly odd. The example below works well, but I do not understand why I need to specify using sysTrajectory::sysTrajectory and not using sysTrajectory<Real>::sysTrajectory<Real> when inheriting from a class template? The latter gives the following error: expected ‘;’ before ‘<’ token using sysTrajectory<Real>::sysTrajectory<Real>;.

class sysRealTrajectory: public sysTrajectory<Real>
{

    public:

    /**
        *   Default constructor
        */
        inline sysRealTrajectory(void);

        using sysTrajectory::sysTrajectory;     

        /**
        *   Default destructor
        */
        inline ~sysRealTrajectory(void);
};

main :

Real a;
a=5;
sysTrajectoryPoint<Real> TP0(1.0,a);
sysRealTrajectory Trajectory(TP0);

回答1:


This syntax

using sysTrajectory::sysTrajectory; 

Names all constructors of sysTrajectory. This syntax

using sysTrajectory::sysTrajectory<Real>;

Names only a constructors that accept a template argument <Real> (yes, you can do that, you can pass explicit template arguments to constructors in declarative contexts). Your base class does not appear to have any constructor templates, so your compiler's parser does not take sysTrajectory as a template-name and hence does not accept the < as an opening template argument list. Hence the syntax error.

For a template-name to be explicitly qualified by the template arguments, the name must be known to refer to a template.

Even if you had a constructor template, a rule for using declarations forbids that too. It says

A using-declaration shall not name a template-id.




回答2:


In gcc 4.8.1 the following syntax works for me:

using sysTrajectory<Real>::sysTrajectory;


来源:https://stackoverflow.com/questions/16760522/c11-inheriting-template-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!