c ++ 0x继承了模板中的构造函数 [英] c++0x inherited constructor in templates

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

问题描述

这里是类foo:

template <typename T>
struct foo
{
    foo()
    {
        t = nullptr;
    }

    foo(T* p, bool flag)
    {
        t = p;
    }
private:
    T* t;
};

以下是类别栏:

template <typename T>
struct bar: public foo<T>
{
    using foo<T>::foo<T>;
};

继承构造函数的语法是否正确?如果我使用using foo :: foo;那么Visual C ++ 2010的编译器就会死机。
所以基本上如何在VC ++ 2010中从模板类继承构造函数?

Is it correct syntax for inheriting constructors? If I use "using foo::foo;" then compiler of Visual C++ 2010 dies. So basically how to inherit constructors from template classes in VC++ 2010?

推荐答案

template <typename T>
struct bar: public foo<T>
{
    using foo<T>::foo<T>;
};

要正确解析,需要插入 template 之前的 foo< T>; ,告诉编译器 foo 模板名称(它不能查看 foo< T> 告诉自己,因为 T 未知)。但在使用声明中不允许使用 :: template 。该名称也不指向 bar 的所有构造函数:相反,它将引用特定的构造函数模板专用化( T 是模板参数)这样的构造函数如下

To let this parse correctly, you would need to insert template before the foo<T>;, to tell the compiler that foo is to be regarded as a template name (it cannot look into foo<T> to tell itself, since T is unknown). But using ::template is not allowed in a using declaration. The name also does not refer to all constructors of bar: Instead, it would refer to a specific constructor function template specialization (T is the template argument) of such a constructor as follows

template<typename T>
foo();

此外,对于使用声明使用 template- id( foo< T> )作为其名称(这实际上禁止它引用函数模板专用化,名称转换函数模板专用化),所以即使你使用 :: template (如果可能的话)修正解析问题,你仍然会出错在这一点。

In addition, it's not valid for a using declaration to use a template-id (like foo<T>) as its name (which in effect forbids it to refer to function template specialization, with the addition of forbidding to name conversion function template specializations stated too), so even if you correct the parsing problem using ::template (if it would be possible), you would still error out at this point.

当引入继承的构造函数时,添加了允许使用句法规则引用构造函数的特殊规则:如果你有一个qualified-id ... :: ... ),最后一个限定在最后一个部分命名一个特定的类之前,那么你可以表示该类的构造函数在两个附加方式:

When inherited constructors were introduced, special rules were added that allow to reference a constructor using a syntactic rule: If you have a qualified-id (which basically a qualified name using ...::...), and the last qualified before the final part names a specific class, then you can denote the constructor(s) of that class in two additional ways:


  • 如果类使用template-id命名(名称为 foo< T> ; ),并且最后部分匹配模板名称(因此, foo< T> :: foo TTP< T> ; :: TTP TTP 是模板模板参数)。

  • 类名(因此, foo :: foo T :: T / code>是一个模板参数)。

  • If the class was named using a template-id (a name of the form foo<T>) and the final part matches the template name (so, foo<T>::foo or TTP<T>::TTP with TTP being a template template parameter).
  • If the final part matches the class name (so, foo::foo or T::T, with T being a template parameter).

这两个附加规则仅在使用声明中有效。它们自然不存在于C ++ 03中。在C ++ 03中也存在的另一个规则是:如果最后一部分命名注入的类名,那么这个限定名也引用构造函数:

These two additional rules are only active in a using declaration. And they were naturally not present in C++03. The other rule that was also present in C++03 is: If the final part names the injected class name, then this qualified name also refers to the constructor:


  • foo :: foo 会为此工作。但是只有这个规则, T :: T (其中 T 表示类 foo 没有成员 T ,因此 foo

  • foo::foo would therefor work. But with this rule alone, T::T (where T denotes class foo) would not work, because foo has no member called T.

因此,根据特殊规则,您可以写

Therefor, with the special rules in place you can write

using foo<T>::foo;
using bar::foo::foo; // valid too

第二个也有效: foo 是被注入到基类 foo< T> 中并且继承到 bar 的注入类名。我们通过 bar :: foo 引用该名称,然后添加最后一个部分 foo ,它指的是注入类名,以表示`foo的构造函数。

The second is valid too: foo is the injected class name which was injected into the base class foo<T> and inherited to bar. We refer to that name by bar::foo, and then add the last part foo, which refers to the injected class name again, to denote the constructor(s) of `foo.

现在你明白为什么你尝试的初始名称会引用一个构造函数模板专门化(如果允许的话):因为 foo< T> :: foo 部分将命名所有构造函数,然后将遵循的< T> 类型参数。

Now you understand why the initial name you tried would refer to a constructor function template specialization (if it were to be allowed to): Because the foo<T>::foo part would name all constructors, and the <T> that would follow would then filter out the template and pass the type argument.

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

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