why List<ColorPtr> error2; is not corrent?

B

baumann@pan

hi all,

Local classes and enumerations (in other words, types declared in a
function definition) cannot be involved in template type arguments.

Types that involve unnamed class types or unnamed enumeration types
cannot be template type arguments (unnamed classes or enumerations that
are given a name through a typedef declaration are OK).

An example illustrates these two exceptions:

template <typename T> class List {
...
};

typedef struct {
double x, y, z;
} Point;

typedef enum { red, green, blue } *ColorPtr;

int main()
{
struct Association
{
int* p;
int* q;
};
List<Assocation*> error1; // ERROR: local type in template
argument
List<ColorPtr> error2; // ERROR: unnamed type in template
// argument
List<Point> ok; // OK: unnamed class type named through
// a typedef
}


i think ColorPrt is the name for the unamed type enum { red, green,
blue} * through typedef declaration.
 
V

Victor Bazarov

baumann@pan said:
Local classes and enumerations (in other words, types declared in a
function definition) cannot be involved in template type arguments.

Yes, they have no linkage.
Types that involve unnamed class types or unnamed enumeration types
cannot be template type arguments (unnamed classes or enumerations that
are given a name through a typedef declaration are OK).

Yes, since they have linkage (usually external) and have name if you use
a typedef.
An example illustrates these two exceptions:

template <typename T> class List {
...
};

typedef struct {
double x, y, z;
} Point;

typedef enum { red, green, blue } *ColorPtr;

int main()
{
List<ColorPtr> error2; // ERROR: unnamed type in template
// argument
}


i think ColorPrt is the name for the unamed type enum { red, green,
blue} * through typedef declaration.

ColorPtr is a _pointer_ to an unnamed type. The type a value of ColorPtr
points to is still *unnamed*. 14.3.1 prohibits the use of unnamed types
_and_ the types compound from them.

V
 
B

baumann@pan

Victor said:
Yes, they have no linkage.


Yes, since they have linkage (usually external) and have name if you use
a typedef.


ColorPtr is a _pointer_ to an unnamed type. The type a value of ColorPtr
points to is still *unnamed*. 14.3.1 prohibits the use of unnamed types
_and_ the types compound from them.

V

if i write the codes as below
typedef enum { red, green, blue } Color;
typedef Color* ColorPtr;


List<ColorPtr> error2;

i think it will work, is not it?
 
V

Victor Bazarov

baumann@pan said:
[..]
if i write the codes as below
typedef enum { red, green, blue } Color;
typedef Color* ColorPtr;


List<ColorPtr> error2;

i think it will work, is not it?

It should. And I just confirmed it with Comeau online.

V
 

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,203
Messages
2,571,059
Members
47,668
Latest member
SamiraShac

Latest Threads

Top