Global templates and namespaces

Y

Yevgen Muntyan

Hello all,

I have a piece of code which compiles with gcc-3.4 but not with intel compiler 8.1. If I correct
the code for intel compiler, then gcc refuses to compile it. The code is following:

////////////////////////////////// test.cpp
template <typename T>
void func ()
{
static T instance;
}

namespace blah {

class MyClass {
friend void func<MyClass> ();

MyClass () {} // private
};

}


int main ()
{
func<blah::MyClass>();
}
////////////////////////////// end of test.cpp

Error is in the 'friend void func<MyClass> ();' line. When I compile this file with
intel compiler, it says

test.cpp(4): error #373: "blah::MyClass::MyClass()" is inaccessible
static T instance;
^
detected during instantiation of "void func<T>() [with T=blah::MyClass]"

To make the compiler happy I change MyClass to

class MyClass {
friend void ::func<MyClass> (); // note the :: before func

MyClass () {}
};

But when g++ says
test2.cpp:11: error: `void func()' should have been declared inside `::'.

What's correct? I tried Comeau compiler on comeaucomputing.com, it compiles both
versions. Is this just two different bugs of g++ and intel compiler?

Thank you,
Yevgen
 
G

Gianni Mariani

Yevgen Muntyan wrote:
...
What's correct? I tried Comeau compiler on comeaucomputing.com, it
compiles both
versions. Is this just two different bugs of g++ and intel compiler?

I think it's two different bugs and Comeau is right (again).
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top