Why doesn't this compile?

N

narechk

The following fails under VC8 Beta2:


namespace A {

// forward decl.
template <typename Type, typename TypeX>
bool foo(const Type&, const TypeX&);

} // namespace A


namespace A {
namespace B {

template <typename Type>
struct S
{
template <typename Type, typename TypeX>
friend bool ::A::foo(const Type&, const TypeX&); // error C2888

protected:
template <typename TypeX>
static bool bar(const Type& a, const TypeX& b)
{
return a == b;
}
};

} // namespace B

template <typename Type, typename TypeX>
bool foo(const Type& a, const TypeX& b)
{
return B::S<Type>::bar(a, b);
}

} // namespace A


int main()
{
}



The error message is:

..\Test.cpp(114) : error C2888: 'bool A::foo(const Type &,const TypeX
&)' : symbol cannot be defined within namespace 'B'
..\Test.cpp(122) : see reference to class template instantiation
'A::B::S<Type>' being compiled

Can someone explain why?


Thanks,

- NK
 
J

John Carson

narechk said:
The following fails under VC8 Beta2:


namespace A {

// forward decl.
template <typename Type, typename TypeX>
bool foo(const Type&, const TypeX&);

} // namespace A


namespace A {
namespace B {

template <typename Type>
struct S
{
template <typename Type, typename TypeX>
friend bool ::A::foo(const Type&, const TypeX&); // error C2888

protected:
template <typename TypeX>
static bool bar(const Type& a, const TypeX& b)
{
return a == b;
}
};

} // namespace B

template <typename Type, typename TypeX>
bool foo(const Type& a, const TypeX& b)
{
return B::S<Type>::bar(a, b);
}

} // namespace A


int main()
{
}



The error message is:

.\Test.cpp(114) : error C2888: 'bool A::foo(const Type &,const TypeX
&)' : symbol cannot be defined within namespace 'B'
.\Test.cpp(122) : see reference to class template instantiation
'A::B::S<Type>' being compiled

Can someone explain why?

There appears to be a bug:

http://lab.msdn.microsoft.com/produ...edbackid=8cb3f12a-f7b4-4d45-a8f2-d67ccd795fc7
 
A

Alf P. Steinbach

* narechk:
Compiles fine with gcc 3.4.2.

Also compiles fine with Comeau Online when the struct S template parameter
is renamed to e.g. "LType" (and correspondingly for S::bar).

Otherwise Comeau detects a "template parameter "Type" may not be redeclared
in this scope", but the fix for Comeau doesn't help with Visual C++ 7.1 (I
don't have the 8.0 beta 2).
 
A

Alf P. Steinbach

* narechk:
Right. This is not good. Does anyone know if there is a workaround?

E.g. place foo in namespace B and just a forwarding foo in namespace A.
 
N

narechk

Everyone thanks for your help. This is a real bloody showstopper. Will
have to redesign.

- NK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top