illegal explicit template specialization

Discussion in 'C++' started by stain, May 29, 2006.

  1. stain

    stain Guest

    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?
     
    stain, May 29, 2006
    #1
    1. Advertising

  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;}
     
    Marek Vondrak, May 29, 2006
    #2
    1. Advertising

  3. stain

    Rolf Magnus Guest

    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;}
     
    Rolf Magnus, May 29, 2006
    #3
  4. stain

    stain Guest

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


    Thanks! That solved it!
     
    stain, May 30, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.T. Conklin
    Replies:
    1
    Views:
    443
    David Hilsee
    Aug 11, 2004
  2. Patrick Kowalzick
    Replies:
    0
    Views:
    819
    Patrick Kowalzick
    Oct 29, 2004
  3. Andy
    Replies:
    5
    Views:
    509
    Shezan Baig
    Jan 30, 2005
  4. Barry
    Replies:
    2
    Views:
    365
    Barry
    Sep 30, 2007
  5. Bill
    Replies:
    7
    Views:
    401
    Marcel Müller
    Feb 22, 2008
Loading...

Share This Page