473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 2417
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
5
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! ...
6
by: Vyacheslav Lanovets | last post by:
Hello, All! I know that Explicit Instantiation actually emits code to obj files (so you can even export them from the module as plain functions or classes). But I found that MSVC7.1 compiler...
8
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
1
by: Thomas Barnet-Lamb | last post by:
I have a query concerning the handling of explicit specializations. Essentially, I want to know whether it is legal to take a template like >template<class X> typename X::A Foo(typename X::B,...
3
by: Hendrik Schober | last post by:
Hi, both Comeau and CW9 complain about the following code. VC71 accepts it. I cannot cite chapter and verse of the standard disallowing this, but I usually believe what Comeau says. Is that a...
1
by: JohnPantango | last post by:
Can anyone give me a simple work around to fix this compiler error on VC7.1 .....cpp(23) : error C2912: explicit specialization; 'bool Equal<_F32>(const _F32::type,const _F32::type)' is not a...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.