简体   繁体   English

模板中的模板默认参数

[英]template default argument in a template

I am trying to compile this:我正在尝试编译这个:

template <class T, class U = myDefaultUClass<T> >
class myClass{
 ...
};

Although it seems quite intuitive to me it is not for my compiler, does anyone knows how to do this?尽管对我来说这似乎很直观,但它不适用于我的编译器,但有人知道该怎么做吗?

edit: Ok, the problem was not actually coming from this but from a residual try... Sorry about this, thanks for your answers anyway.编辑:好的,问题实际上不是来自于此,而是来自残余尝试......对此感到抱歉,无论如何感谢您的回答。

The following works for me using g++.以下适用于我使用 g++。 Please post more code, the error messages you are getting and the compiler version.请发布更多代码、您收到的错误消息和编译器版本。

class A {};

template <class T> class T1 {};

template <class T, class U = T1<T> > class T2 {
};

T2 <A> t2;

Compiles fine with Comeau ...Comeau编译得很好......

This works on MSVC 9.0:这适用于 MSVC 9.0:

template < class T >
class UClass
{
private:
    T m_data;
};

template < class T, class U = UClass< T > >
class MyClass 
{
public:
    const U& data() const { return m_data; }
private:

    U m_data;
};


int main()
{
    MyClass< int > test;

    const UClass<int>& u = test.data();

    return 0;
}

It's either that your compiler isn't standard complaint, or you made one of these mistakes:要么你的编译器不是标准的抱怨,要么你犯了以下错误之一:

  1. myDefaultUClass is not a template myDefaultUClass 不是模板
  2. myDefaultUClass isn't defined myDefaultUClass 未定义

because the following works fine in G++:因为以下在 G++ 中工作正常:

class myDefaultUClass{};

template <class T, class U = myDefaultUClass >
class myClass{
 //...
};

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

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