470,815 Members | 1,738 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

illegal explicit template specialization

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?

May 29 '06 #1
3 2253
stain wrote:
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;}
May 29 '06 #2
> template <>
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;}
May 29 '06 #3
> Try:

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


Thanks! That solved it!

May 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Barnet-Lamb | last post: by
3 posts views Thread by Hendrik Schober | last post: by
reply views Thread by greek_bill | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.