By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

Why doesn't this compile?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"narechk" <na*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com
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/produc...2-d67ccd795fc7
--
John Carson

Jul 23 '05 #2

P: n/a
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3

P: n/a
Right. This is not good. Does anyone know if there is a workaround?

Thanks,

- NK

Jul 23 '05 #4

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5

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

- NK

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.