VC++ 2003 template bug? 
Author Message
 VC++ 2003 template bug?

This example will not compile in VC++ 2003 unless I use the inline versions
of the setbar() and getbar() functions.  Both versions work in VC++ 2002.
Is there something wrong with the external definition of these functions?
Perhaps I'm missing something.

template<class T>
class foo
{
public:
 struct bar
 {
  int value;
 };
 foo() : mbar( 0 ) { }
 ~foo() { delete mbar; mbar = 0; }
 void setbar(); // { delete mbar; mbar = new bar; }
 bar* getbar(); // { return mbar; }
 bar* mbar;
 T member;

Quote:
};

template<class T>
void foo<T>::setbar()
{
 delete mbar;
 mbar = new bar;

Quote:
}

template<class T>
foo<T>::bar* foo<T>::getbar()
{
 return mbar;

Quote:
}

int _tmain(int argc, _TCHAR* argv[])
{
 foo<int> f;
 f.setbar();
 foo<int>::bar* b = f.getbar();
 return 0;

Quote:
}

I own 2002 and am waiting for the $29 2003 upgrade to arrive.  Meanwhile I'm
using the 2003 trial DVD I received at the launch event.

Mark



Mon, 24 Oct 2005 14:42:56 GMT  
 VC++ 2003 template bug?

Quote:

> This example will not compile in VC++ 2003 unless I use the inline
> versions of the setbar() and getbar() functions.  Both versions work
> in VC++ 2002. Is there something wrong with the external definition
> of these functions? Perhaps I'm missing something.

The problem lies exclusively with getbar(), and you're missing something,
and the error message is telling you exactly what you're missing - you
nneed to use the 'typename' keyword in the declaration of the return type,
e.g.:

template<class T>
typename foo<T>::bar* foo<T>::getbar()
{
 return mbar;

Quote:
}

As to whether this is standard, I'm not convinced.  In general, VC7.1's
enforcement of the use of typename is standard conformant, but in this
particular case, I think the compiler might be in error.  Since foo<T> is
the class whose member is being defined, foo<T>::bar is not really a
dependent name (which is why it works without typename in the inline case).

Comeau agrees with my hunch, and accepts the code with or without typename.

-cd



Mon, 24 Oct 2005 21:31:08 GMT  
 VC++ 2003 template bug?
Quote:
> As to whether this is standard, I'm not convinced.  In general, VC7.1's
> enforcement of the use of typename is standard conformant, but in this
> particular case, I think the compiler might be in error.  Since foo<T> is
> the class whose member is being defined, foo<T>::bar is not really a
> dependent name (which is why it works without typename in the inline

case).

The reason why it works in the inline case is because the return type isn't
a qualified-id, hence typename isn't allowed.

Quote:
> Comeau agrees with my hunch, and accepts the code with or without

typename.

According to the standard, VC7.1 is right. However, there is merit to your
hunch. A proposed DR,
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#224 , has been
submitted which would make your hunch valid.

Bo-Staffan



Tue, 25 Oct 2005 09:41:20 GMT  
 VC++ 2003 template bug?
Thanks guys.  I am now more knowledgable.
Mark


Wed, 26 Oct 2005 11:43:29 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. VC++ 2003 c2.dll bug D2029

2. VC .NET 2003 l-value bug?

3. 2 bugs in VC++ 2003

4. VC++ 2003 compiler bug

5. template template parameter with default value (VS.NET 2003)

6. VC.NET 2003 IDE bug report

7. Very probably a bug in the code generator of VC++ 2003

8. VC 7.1 final beta: bug with template template parameters

9. Possible bug with template template parameters in VC++ 7.0

10. Problem porting ATL project from VC++ 6.0 to VC++.NET 2003

11. Converting project from VC++ 2002 to VC++ 2003 and adding a new configuration

12. Slower compilation with VC Net 2003 (7.1) when compared to VC Net 7.0

 

 
Powered by phpBB® Forum Software