c ++ 11继承模板构造函数 [英] c++11 inheriting template constructors

查看:484
本文介绍了c ++ 11继承模板构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++ 11中找到构造函数继承功能非常有用。但是,我发现它的语法有点奇怪。下面的例子工作正常,但我不明白为什么我需要指定使用sysTrajectory :: sysTrajectory ,而不是使用sysTrajectory< Real> ::当从模板类继承时,sysTrajectory< Real> ?后者给出以下错误:expected';'before'<'token using sysTrajectory :: sysTrajectory ;.从类模板继承构造函数时,是否会有任何潜在的问题?

I find constructor inheritance feature in c++11 quite useful. However, I find its syntax slightly odd. The example below works fine, but I do not understand why I need to specify using sysTrajectory::sysTrajectory as opposed to using sysTrajectory<Real>::sysTrajectory<Real> when inheriting from a template class? The latter gives the following error: expected ‘;’ before ‘<’ token using sysTrajectory::sysTrajectory;. Could there be any potential problems when inheriting constructors from a class template?

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 AAA(TP0);


推荐答案

此语法

using sysTrajectory::sysTrajectory; 

命名 sysTrajectory 的所有构造函数。此语法

Names all constructors of sysTrajectory. This syntax

using sysTrajectory::sysTrajectory<Real>;

仅命名接受模板参数的构造函数< Real> code>(是的,你可以这样做,你可以传递显式模板参数到声明上下文中的构造函数)。你的基类似乎没有任何构造函数模板,所以你的编译器的解析器不会将 sysTrajectory 作为模板名,因此不接受 < $ / code>作为打开模板参数列表。因此语法错误。

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.

这篇关于c ++ 11继承模板构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆