Member function specialization workarounds?

Discussion in 'C++' started by Alex Pavloff, May 10, 2005.

  1. Alex Pavloff

    Alex Pavloff Guest

    Hi folks,

    Here's a piece of example code describing the issue I'm having:

    struct Foo
    {
    template<int N>
    int Blah()
    {
    return 1;
    }

    template<>
    int Blah<0>()
    {
    return 0;
    }
    };

    Microsoft Visual C++ 7.1 compiles and runs this as expected.

    Foo f;
    int i = f.Blah<1>(); // i == 1
    int j = f.Blah<0>(); // j == 0

    gcc 3.3.1 however, doesn't like compiling it, giving...

    error: explicit specialization in non-namespace scope `struct Foo'

    ....in addition to numerous other errors resulting because the compiler is
    now confused.

    Googling for this finds various discussions of what's legal and what's not
    when it comes to member function specialization, but I've got a little more
    practical question. What sort of workaround can I use to make this code do
    pretty much the same thing and run on gcc 3.3.1?

    Thanks,

    Alex Pavloff
     
    Alex Pavloff, May 10, 2005
    #1
    1. Advertising

  2. Alex Pavloff wrote:
    > Here's a piece of example code describing the issue I'm having:
    >
    > struct Foo
    > {
    > template<int N>
    > int Blah()
    > {
    > return 1;
    > }
    >
    > template<>
    > int Blah<0>()
    > {
    > return 0;
    > }
    > };
    >
    > Microsoft Visual C++ 7.1 compiles and runs this as expected.
    >
    > Foo f;
    > int i = f.Blah<1>(); // i == 1
    > int j = f.Blah<0>(); // j == 0
    >
    > gcc 3.3.1 however, doesn't like compiling it, giving...
    >
    > error: explicit specialization in non-namespace scope `struct Foo'
    >
    > ...in addition to numerous other errors resulting because the compiler is
    > now confused.
    >
    > Googling for this finds various discussions of what's legal and what's not
    > when it comes to member function specialization, but I've got a little more
    > practical question. What sort of workaround can I use to make this code do
    > pretty much the same thing and run on gcc 3.3.1?



    Move the definition of Blah<0> outside the class:

    struct Foo
    {
    template<int N>
    int Blah()
    {
    return 1;
    }
    };


    template<>
    int Foo::Blah<0>()
    {
    return 0;
    }

    int main() {

    Foo f;
    int i = f.Blah<1>(); // i == 1
    int j = f.Blah<0>(); // j == 0
    }

    V
     
    Victor Bazarov, May 10, 2005
    #2
    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. Replies:
    3
    Views:
    822
    MattC
    Jan 14, 2005
  2. Marcus
    Replies:
    2
    Views:
    620
    Marcus
    Dec 9, 2005
  3. D Yuniskis

    Preprocessor limitation workarounds

    D Yuniskis, Nov 22, 2009, in forum: C Programming
    Replies:
    32
    Views:
    1,130
    D Yuniskis
    Nov 26, 2009
  4. jason
    Replies:
    2
    Views:
    202
    jason
    Aug 7, 2003
  5. liquid_rails
    Replies:
    11
    Views:
    216
    Michal Suchanek
    Jul 2, 2008
Loading...

Share This Page