illegal explicit template specialization

S

stain

hi,

the following code produces an "illegal explicit template
specialization" warning. I have been fiddling around a while to find a
solution to specialize a template method of a specialized template
class. Here is what I would like to be able to call:

ChannelIndexTraits<RGB>::index<RED>() == 0
ChannelIndexTraits<RGB>::index<GREEN>() == 1
ChannelIndexTraits<RGB>::index<ALPHA>() == -1
ChannelIndexTraits<RGBA>::index<ALPHA>() == 3
ChannelIndexTraits<RGBA>::index<RED>() == 0

the following code produces the warning:

enum ColorComponents {
RGB,
RGBA
};

enum ChannelType {
ALPHA,
RED,
GREEN,
BLUE
};

template <ColorComponents>
struct ChannelIndexTraits {
template <ChannelType TYPE>
static int index() {return -1;}
};

// explicit template specializations to map channels to colorcomponents

template <>
static int ChannelIndexTraits<RGB>::index<RED>() {return 0;}

template <>
static int ChannelIndexTraits<RGB>::index<GREEN>() {return 1;}

template <>
static int ChannelIndexTraits<RGB>::index<BLUE>() {return 2;}

template <>
static int ChannelIndexTraits<RGBA>::index<RED>() {return 0;}

template <>
static int ChannelIndexTraits<RGBA>::index<GREEN>() {return 1;}

template <>
static int ChannelIndexTraits<RGBA>::index<BLUE>() {return 2;}

template <>
static int ChannelIndexTraits<RGBA>::index<ALPHA>() {return 3;}


Any Ideas?
 
M

Marek Vondrak

template said:
static int ChannelIndexTraits<RGB>::index<RED>() {return 0;}

This is wrong. You have to provide explicit specializations for
ChannelIndexTraits<RGB> and ChannelIndexTraits<RGBA> first. Once that is
done, you specialize ChannelIndexTraits<RGB>'s functions and
ChannelIndexTraits<RGBA>'s functions.

enum ColorComponents {
RGB,
RGBA
};

enum ChannelType {
ALPHA,
RED,
GREEN,
BLUE
};

template <ColorComponents>
struct ChannelIndexTraits {
template <ChannelType TYPE>
static int index() {return -1;}
};

// specialize ChannelIndexTraits first

template <>
struct ChannelIndexTraits<RGB> {
template <ChannelType TYPE>
static int index() { return -1; }
};

template <>
struct ChannelIndexTraits<RGBA> {
template <ChannelType TYPE>
static int index() { return -1; }
};

// specialize ChannelIndexTraits<RGB>::index()

template <>
int ChannelIndexTraits<RGB>::index<RED>() {return 0;}

template <>
int ChannelIndexTraits<RGB>::index<GREEN>() {return 1;}

template <>
int ChannelIndexTraits<RGB>::index<BLUE>() {return 2;}

// specialize ChannelIndexTraits<RGBA>::index()

template <>
int ChannelIndexTraits<RGBA>::index<RED>() {return 0;}

template <>
int ChannelIndexTraits<RGBA>::index<GREEN>() {return 1;}

template <>
int ChannelIndexTraits<RGBA>::index<BLUE>() {return 2;}

template <>
int ChannelIndexTraits<RGBA>::index<ALPHA>() {return 3;}
 
R

Rolf Magnus

stain said:
hi,

the following code produces an "illegal explicit template
specialization" warning. I have been fiddling around a while to find a
solution to specialize a template method of a specialized template
class. Here is what I would like to be able to call:

ChannelIndexTraits<RGB>::index<RED>() == 0
ChannelIndexTraits<RGB>::index<GREEN>() == 1
ChannelIndexTraits<RGB>::index<ALPHA>() == -1
ChannelIndexTraits<RGBA>::index<ALPHA>() == 3
ChannelIndexTraits<RGBA>::index<RED>() == 0

the following code produces the warning:

enum ColorComponents {
RGB,
RGBA
};

enum ChannelType {
ALPHA,
RED,
GREEN,
BLUE
};

template <ColorComponents>
struct ChannelIndexTraits {
template <ChannelType TYPE>
static int index() {return -1;}
};

// explicit template specializations to map channels to colorcomponents

template <>
static int ChannelIndexTraits<RGB>::index<RED>() {return 0;}
[...]

Try:

template <>
template <>
int ChannelIndexTraits<RGB>::index<RED>() {return 0;}
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top