Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在子类中定义或重新定义模板嵌套类?_C++_Templates - Fatal编程技术网

C++ 如何在子类中定义或重新定义模板嵌套类?

C++ 如何在子类中定义或重新定义模板嵌套类?,c++,templates,C++,Templates,考虑以下几点: template <typename T> class Base { public: template <typename U> class Nested { }; }; template <typename T> class Derived : public Base<T> { public: //How do we typedef of redefine Base<T>::Neste

考虑以下几点:

template <typename T>
class Base {
  public:
    template <typename U>
    class Nested { };
};

template <typename T>
class Derived : public Base<T> {
  public:
    //How do we typedef of redefine Base<T>::Nested?
    using Base<T>::Nested; //This does not work
    using Base<T>::template<typename U> Nested; //Cannot do this either
    typedef typename Base<T>::template<typename U> Nested Nested; //Nope..

    //now we want to use the Nested class here
    template <typename U>
    Class NestedDerived : public Nested { };

    //or like this:
    Nested<int> nestedVar; // obviously does not work
};
模板
阶级基础{
公众:
模板
类嵌套{};
};
模板
派生类:公共基{
公众:
//如何重新定义Base::Nested的typedef?
使用Base::Nested;//这不起作用
使用Base::template嵌套;//也不能这样做
typedef typename Base::模板嵌套;//否。。
//现在我们想在这里使用嵌套类
模板
嵌套派生类:公共嵌套{};
//或者像这样:
嵌套的nestedVar;//显然不起作用
};
如何在派生类中使用模板化嵌套类?这是否可能在当前版本的C++标准中执行?

< P>尝试:< /P>
template <typename T>
class Base {
  public:
    template <typename U>
    class Nested { };
};

template <typename T>
class Derived : public Base<T> {
  public:
    //How do we typedef of redefine Base<T>::Nested?
    //using Base<T>::Nested; //This does not work
  //using Base<T>::template<typename U> Nested; //Cannot do this either
  //typedef typename Base<T>::template<typename U> Nested Nested; //Nope..

    //now we want to use the Nested class here
  template <typename U>
  class NestedDerived : public Base<T>::template Nested<U> { };
};

int main()
{
  Base<int>::Nested<double> nested;

  Derived<int>::NestedDerived<double> nested_derived;

  return 0;
}
模板
阶级基础{
公众:
模板
类嵌套{};
};
模板
派生类:公共基{
公众:
//如何重新定义Base::Nested的typedef?
//使用Base::Nested;//这不起作用
//使用Base::template嵌套;//也不能这样做
//typedef typename Base::模板嵌套;//否。。
//现在我们想在这里使用嵌套类
模板
类NestedDerived:public Base::template Nested{};
};
int main()
{
Base::嵌套;
派生::NestedDerived嵌套_派生;
返回0;
}

在slackware 13上使用gcc 4.3.3编译得很好,我仍然不能100%确定您想要什么,但您可以试试。
这是在VisualStudio上编译的

template <typename T>
class Base {
  public:
    template <typename U>
    class Nested { };
};

template <typename T>
class Derived : public Base<T> {
  public:
    //now we want to use the Nested class here
    template <typename U>
    class NestedDerived : public Nested<U> { };
};

int _tmain(int argc, _TCHAR* argv[])
{
Base<int>::Nested<double> blah2;
Derived<int>::NestedDerived<int> blah;

return 0;
}
模板
阶级基础{
公众:
模板
类嵌套{};
};
模板
派生类:公共基{
公众:
//现在我们想在这里使用嵌套类
模板
嵌套派生类:公共嵌套{};
};
int _tmain(int argc,_TCHAR*argv[]
{
Base::嵌套blah2;
派生::嵌套派生的废话;
返回0;
}
这似乎有效:
(编辑:添加了更多行以显示第一个模板语句。感谢Samir Talwar更正了我的格式!)

模板
派生类:公共基{
公众:
typedef typename Base::模板嵌套;
嵌套派生类:公共嵌套{};
嵌套的nestedVar;
};

实际上
使用
就像广告宣传的那样有效,它只是没有消除模板中的依赖名称问题,而且当前无法直接别名模板(将是):

模板
结构基{
模板结构嵌套{};
};
模板
派生结构:基{
使用Base::Nested;
//需要使用模板嵌套前缀,因为
//它是一个依赖模板:
结构X:Base::模板嵌套{};
//这里也一样:
模板
struct Y:Base::template嵌套{};
//数据成员,此处需要typename:
typename Base::模板嵌套数据;
};
void f(){
派生::嵌套n;//在外部可以正常工作
}
在模板中使用
Derived::Nested
时还有另一个可能的问题,但这也是一个依赖名称问题,与继承无关:

template<class T>
void g() {
    // Nested is a dependent type and a dependent template, thus
    // we need 'typename' and 'template':
    typedef typename Derived<T>::template Nested<int> NestedInt;
}
模板
void g(){
//Nested是一种依赖类型和依赖模板,因此
//我们需要“typename”和“template”:
typedef typename派生::模板嵌套嵌套;
}
请记住,依赖于模板参数的名称必须是

  • 如果是从属类型,则前缀为
    typename
    typename a::B
  • 如果是从属模板,则直接以
    template
    作为前缀:
    a::template f()
  • 如果两者都有:
    typename A::template B
  • typename
    在基类列表中是非法的:
    template结构A:B,C::template D{}

我不明白为什么要将嵌套类设置为“派生”类,因为派生类无论如何都会继承嵌套类。我希望“嵌套”类封装在“Base”中,“派生”类可以扩展或使用“Base::Nested”类。在什么情况下,
使用Base::Nested不工作?@gf:在我的示例的最后一行:
nestedVar。我不能仅仅用一个单词
Nested
来使用/引用嵌套类,尽管我已经使用了
using Base::Nested
对不起,但这不是我要问的。我想知道的是,是否可以在
派生的
类中键入def或使用嵌套的
public Base::template
,因为该类型将在很多地方使用;我做的和克雷格做的有什么区别?我真的没有发现:/问题是你不能像在派生类中那样使用嵌套定义,你必须使用一个完全限定的名称。我看到的唯一区别是这个行类嵌套派生:public Base::template Nested{};区别在于coelhudo的解决方案是在gcc/g++上编译的,但仍然没有回答我的问题,但现在用户总是必须为
派生的
指定第二个模板参数。是的,一个派生的不能有多个嵌套派生的类型。这是一个严重的限制,但除此之外,我看不到任何让NestedDerived使用Base::Nested的方法。您能举个例子说明如何为派生类的数据成员使用嵌套类型吗?如果我们仍然必须使用
Base::
,那么使用Base::Nested的
会没有用吗?可以<代码>使用Base::Nested
并不是无用的,它在
f()
g()
中使用-如果没有
使用
声明,您必须通过
f()
中的
Base
访问
Nested
。回答得好+1.值得指出的是,如果
Nested
是一个类型名,则会使用另一个版本
使用typename
(然后C++0x将
Nested
派生的
范围内分类为一个类型名(C++03错过了这个分类,即使支持
使用typename
!)。遗憾的是,没有办法只使用Base::templat编写

template <class T>
struct Base {
    template <class U> struct Nested {};
};

template <class T>
struct Derived : Base<T> {
    using Base<T>::Nested;

    // need to prefix Nested with template because
    // it is a dependent template:
    struct X : Base<T>::template Nested<int> {};

    // same here:
    template<class U>
    struct Y : Base<T>::template Nested<U> {};

    // data member, typename is needed here:
    typename Base<T>::template Nested<int> data;
};

void f() { 
    Derived<int>::Nested<int> n; // works fine outside
}
template<class T>
void g() {
    // Nested is a dependent type and a dependent template, thus
    // we need 'typename' and 'template':
    typedef typename Derived<T>::template Nested<int> NestedInt;
}