By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,149 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,149 IT Pros & Developers. It's quick & easy.

illegal explicit template specialization

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
> 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

P: n/a
> 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.