template class and default function parameter

Q

Qingning Huo

[Repost to comp.lang.c++]

Hi,

Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun
CC 5.8.

--cut --
template<class T>
class TClass
{
public:
void f(int x = T::EnumID);

};

class Unknown;

class X
{
public:
TClass<Unknown> m;
};
-- cut --

The error message of Sun CC is:

-- cut --
"enumid.cc", line 5: Error: EnumID is not a member of Unknown.
"enumid.cc", line 13: Where: While specializing "TClass<Unknown>".
"enumid.cc", line 13: Where: Specialized in non-template code.
1 Error(s) detected.
-- cut --

Defining class Unknown before class X makes it compile on Sun CC. I am
wondering which compiler is correct?

Qingning
 
K

Kai-Uwe Bux

Qingning said:
[Repost to comp.lang.c++]

Hi,

Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun
CC 5.8.

--cut --
template<class T>
class TClass
{
public:
void f(int x = T::EnumID);

};

class Unknown;

class X
{
public:
TClass<Unknown> m;
};
-- cut --

Whether this is legal or not depends on how you continue. This is legal:


template<class T>
class TClass {
public:
void f(int x = T::EnumID);
};

class Unknown;

class X {
public:
TClass<Unknown> m;
};

struct Unknown {
static int const EnumID=0;
};

void dummy ( X x ) {
x.m.f();
}

int main ( void ) {}


This is not:

template<class T>
class TClass {
public:
void f(int x = T::EnumID);
};

class Unknown;

class X {
public:
TClass<Unknown> m;
};

void dummy ( X x ) {
x.m.f();
}

struct Unknown {
static int const EnumID=0;
};

int main ( void ) {}



I think, what is important is the point of instantiation of the member
function x.m.f(). The definition of Unknown has to be known at that point.


Best

Kai-Uwe Bux
 
Q

Qingning

Kai-Uwe Bux said:
Qingning said:
[Repost to comp.lang.c++]

Hi,

Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun
CC 5.8.

--cut --
template<class T>
class TClass
{
public:
void f(int x = T::EnumID);

};

class Unknown;

class X
{
public:
TClass<Unknown> m;
};
-- cut --

Whether this is legal or not depends on how you continue.

Thanks for your reply. The problem is that CC reports error as soon as
it sees the definition of "class X". As I understand, adding code
after the point of error would not work in C++.
This is legal:

template<class T>
class TClass {
public:
void f(int x = T::EnumID);
};

class Unknown;

class X {
public:
TClass<Unknown> m;
};

struct Unknown {
static int const EnumID=0;
};

void dummy ( X x ) {
x.m.f();
}

int main ( void ) {}

This code generates the same error.
I think, what is important is the point of instantiation of the member
function x.m.f(). The definition of Unknown has to be known at that point.

I agree with you. But what is the point of instantiation of f()? The
following code declares variable of class X, calls TClass<T>::f(), and
even defines TClass<T>::f() without defining class Unknown. And it
still passes g++.

-- cut --
template<class T>
class TClass
{
public:
void f(int i = T::EnumID);
};

class Unknown;

class X
{
public:
TClass<Unknown> m;
};

X x1;

int g(X& x0)
{
x0.m.f(1);
}

template<class T>
void TClass<T>::f(int i)
{
i = 0;
}
-- cut --

Regards,
Qingning
 

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

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top