简体   繁体   English

c ++ 11继承模板构造函数

[英]c++11 inheriting template constructors

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? 下面的示例运行良好,但我不明白为什么我需要using sysTrajectory::sysTrajectory指定并且在从类模板继承时不using sysTrajectory<Real>::sysTrajectory<Real> The latter gives the following error: expected ';' before '<' token using sysTrajectory<Real>::sysTrajectory<Real>; 后者给出以下错误: expected ';' before '<' token using sysTrajectory<Real>::sysTrajectory<Real>; 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);

This syntax 这个语法

using sysTrajectory::sysTrajectory; 

Names all constructors of sysTrajectory . 命名sysTrajectory所有构造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). 仅命名接受模板参数<Real>的构造函数(是的,您可以这样做,您可以将显式模板参数传递给声明性上下文中的构造函数)。 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. 您的基类似乎没有任何构造函数模板,因此编译器的解析器不会将sysTrajectory作为模板名称,因此不接受<作为开放模板参数列表。 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. using声明不应命名template-id。

在gcc 4.8.1中,以下语法适用于我:

using sysTrajectory<Real>::sysTrajectory;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM