Re: Template problem



"Jens Mander" <jens@xxxxxxxxxx> wrote in message
news:OaXCBvRBHHA.1012@xxxxxxxxxxxxxxxxxxxx
The following code generates errors when compiled using VC++ 7.1.
However, gcc 3.2.2 and comeau online have no problems with it. Is
this illegal code or a compiler bug? If it is the latter; was it
fixed in the latest release of VC++? Are there any workarounds for my
version?

Thank you in advance!

====================
template <typename T>
struct Outer
{
template <T value = 0>
struct Inner {};
};

int main ()
{
Outer<int>::Inner<> fail;
}


1. I'm guessing it is a bug.

2. No, it hasn't been fixed.

3. A workaround is to only forward declare the Inner class when you declare
the Outer class, and to add the full declaration of Inner later.

template <typename T>
struct Outer
{
// forward declare
template <T value = 0>
struct Inner;
};

// complete the declaration
template <typename T>
template<T value>
struct Outer<T>::Inner<value>
{};


int main ()
{
Outer<int>::Inner<> fail;
return 0;
}


--
John Carson


.



Relevant Pages

  • Re: Template Problem
    ... compile in VS2005. ... use the template argument T in 'Inner'? ... struct Inner; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: null assignment in a template
    ... // this template won't compile due to the fact that it is illegal to ... and declare the value in its explicit ... delcares a single ctor to force the client to have to declare an initial value for said variable, as oppossed to Inialisedwhich allows for a compile-time constant initialization value to be specified in the template parameters themselves. ... taking a pointer? ...
    (microsoft.public.vc.stl)
  • Re: Problem overriding >> and << in a template class
    ... the rejection also mentioned the cause the of problem: ... For the friend functions to work correctly you need to declare them ... to use a template argument list in the friend declaration. ...
    (comp.lang.cpp)
  • null assignment in a template
    ... My question is about template programming in C++, ... // due to the fact that it is illegal to declare a template with a non-scalar value ... There is no conversion from int to Widget*! ...
    (microsoft.public.vc.stl)
  • Re: null assignment in a template
    ... My question is about template programming in C++, ... // due to the fact that it is illegal to declare a template with a non-scalar value ... int i = 5; ... That isn't going to compile, since the second arg must be an extern pointer value, or NULL. ...
    (microsoft.public.vc.stl)