Possible g++ bug in template parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rennie deGraaf

    Possible g++ bug in template parsing

    The attached code compiles and works properly when I comment out the
    declaration, definition, and invocations of the method 'eck'. With
    "eck" in there, g++ fails with

    ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck( int)'
    does not match any template declaration
    ttest.cpp:23: syntax error before `{' token
    ttest.cpp:25: syntax error before `<<' token

    Note that 'gak' (a template function in a template class) works, as
    does
    'ugh" (a normal function in a specialized template class); it's only
    'eck', a template function in a specialized template class, that
    fails.

    The compiler was g++ 3.2.2 20030222 on Red Hat 9 i386.

    g++ 2.95.3 on Solaris SPARC 8 failed, with a different error.

    It did compile and run on Solaris 8 using Sun's C++ compiler. This
    doesn't help me much, since the code must run on Linux i386, but
    suggests that the code is probably valid.

    Unfortunately, I don't have access to a system with a latest-release
    gcc to test.

    Do I have a gcc bug, or is this code actually invalid?

    If it is a bug, does anyone know of a workaround I could use for the
    time being? If it's bad code, then what am I doing wrong? Is Sun's
    CC in error compiling this?

    Thanks for the help.


    Rennie deGraaf

    -------------------------------------

    #include <iostream>
    #include <typeinfo>
    using namespace std;

    template <class TYPE>
    class blah
    {
    TYPE feh;
    public:
    void blech(int x);
    template <class T> void gak(int x);endl; }
    template <class T> void eck(int x);
    void ugh(int x);
    };

    template <class TYPE> template <class T> void blah<TYPE>::gak (int x)
    {
    T a;
    cout << typeid(a).name( ) << endl;
    }

    template <> template <class T> void blah<int>::eck( int x)
    {
    T a;
    cout << typeid(a).name( ) << typeid(feh).nam e() << endl;
    }

    template <class TYPE> void blah<TYPE>::ble ch(int x)
    {
    cout << typeid(feh).nam e() << endl;
    }

    template <> void blah<int>::ugh( int x)
    {
    cout << typeid(feh).nam e() << endl;
    }


    /*template <> template <class T> void blah<char>::eck (int x)
    {
    T a;
    cout << "calling blah<char>::eck (int) " << typeid(feh).nam e() <<
    "\n";
    }*/



    template <class T>
    void foobar(int x)
    {
    T a;
    cout << typeid(a).name( ) << endl;
    }

    int main()
    {
    cout << "call foobar with int: ";
    foobar<int>(1);
    cout << "call foobar with char: ";
    foobar<char>(1) ;
    cout << "call foobar with short: ";
    foobar<float>(1 );
    cout << "call foobar with int*: ";
    foobar<int*>(1) ;

    blah<int> a;
    blah<char> b;
    blah<float> c;
    blah<int*> d;

    cout << "call blah<int>.blech (): ";
    a.blech(1);
    cout << "call blah<char>.blec h(): ";
    b.blech(1);
    cout << "call blah<float>.ble ch(): ";
    c.blech(1);
    cout << "call blah<int*>.blec h(): ";
    d.blech(1);

    cout << "call blah<int>.gak<f loat>(): ";
    a.gak<float>(1) ;

    cout << "call blah<int>.ugh() : ";
    a.ugh(1);
    // cout << "call blah<char>.ugh( ): ";
    // b.ugh(1); // this isn't defined, and
    shouldn't work

    cout << "call blah<int>.eck<f loat>(): ";
    a.eck<float>(1) ;
    // cout << "call blah<char>.eck< float>(): ";
    // b.eck<float>(1) ; // this isn't defined, and
    shouldn't work


    return 0;
    }
  • David B. Held

    #2
    Re: Possible g++ bug in template parsing

    "Rennie deGraaf" <rendeg@my-dejanews.com> wrote in message
    news:7cad4b2a.0 310241518.66917 b95@posting.goo gle.com...[color=blue]
    > The attached code compiles and works properly when I comment
    > out the declaration, definition, and invocations of the method 'eck'.
    > With "eck" in there, g++ fails with
    >
    > ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck( int)'
    > does not match any template declaration
    > ttest.cpp:23: syntax error before `{' token
    > ttest.cpp:25: syntax error before `<<' token[/color]

    I think this is correct.
    [color=blue]
    > Note that 'gak' (a template function in a template class) works,
    > as does 'ugh" (a normal function in a specialized template class);
    > it's only 'eck', a template function in a specialized template class,
    > that fails.[/color]
    [color=blue]
    > [...]
    > Do I have a gcc bug, or is this code actually invalid?[/color]

    Bad code.
    [color=blue]
    > If it is a bug, does anyone know of a workaround I could use for
    > the time being? If it's bad code, then what am I doing wrong?
    > Is Sun's CC in error compiling this?[/color]

    Looks like it to me.
    [color=blue]
    > [...]
    > template <class TYPE>
    > class blah
    > {
    > TYPE feh;
    > public:
    > void blech(int x);
    > template <class T> void gak(int x);endl; }[/color]

    This obviously isn't a cut-n-paste, or this line shouldn't compile.
    [color=blue]
    > template <class T> void eck(int x);
    > void ugh(int x);
    > };
    >
    > template <> template <class T> void blah<int>::eck( int x)
    > {
    > T a;
    > cout << typeid(a).name( ) << typeid(feh).nam e() << endl;
    > }
    > [...]
    > template <> void blah<int>::ugh( int x)
    > {
    > cout << typeid(feh).nam e() << endl;
    > }
    > [...][/color]

    At no point do you define the specialization:

    template <>
    class blah<int>
    {
    ...
    };

    Therefore, defining member functions of this specialization
    is illegal. The solution is to define the class specialization.

    Dave



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


    Comment

    • Rennie deGraaf

      #3
      Re: Possible g++ bug in template parsing

      "David B. Held" <dheld@codelogi cconsulting.com > wrote in message news:<bnd65s$ig 7$1@news.astoun d.net>...[color=blue]
      > "Rennie deGraaf" <rendeg@my-dejanews.com> wrote in message
      > news:7cad4b2a.0 310241518.66917 b95@posting.goo gle.com...[color=green]
      > > The attached code compiles and works properly when I comment
      > > out the declaration, definition, and invocations of the method 'eck'.
      > > With "eck" in there, g++ fails with
      > >
      > > ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck( int)'
      > > does not match any template declaration
      > > ttest.cpp:23: syntax error before `{' token
      > > ttest.cpp:25: syntax error before `<<' token[/color]
      >
      > I think this is correct.
      >[color=green]
      > > Note that 'gak' (a template function in a template class) works,
      > > as does 'ugh" (a normal function in a specialized template class);
      > > it's only 'eck', a template function in a specialized template class,
      > > that fails.[/color]
      >[color=green]
      > > [...]
      > > Do I have a gcc bug, or is this code actually invalid?[/color]
      >
      > Bad code.
      >[color=green]
      > > If it is a bug, does anyone know of a workaround I could use for
      > > the time being? If it's bad code, then what am I doing wrong?
      > > Is Sun's CC in error compiling this?[/color]
      >
      > Looks like it to me.
      >[color=green]
      > > [...]
      > > template <class TYPE>
      > > class blah
      > > {
      > > TYPE feh;
      > > public:
      > > void blech(int x);
      > > template <class T> void gak(int x);endl; }[/color]
      >
      > This obviously isn't a cut-n-paste, or this line shouldn't compile.[/color]

      Sorry about that; I deleted a multi-line comment when I posted it, and
      evidently missed part. The rest IS a cut-and-paste.[color=blue]
      >[color=green]
      > > template <class T> void eck(int x);
      > > void ugh(int x);
      > > };
      > >
      > > template <> template <class T> void blah<int>::eck( int x)
      > > {
      > > T a;
      > > cout << typeid(a).name( ) << typeid(feh).nam e() << endl;
      > > }
      > > [...]
      > > template <> void blah<int>::ugh( int x)
      > > {
      > > cout << typeid(feh).nam e() << endl;
      > > }
      > > [...][/color]
      >
      > At no point do you define the specialization:
      >
      > template <>
      > class blah<int>
      > {
      > ...
      > };
      >
      > Therefore, defining member functions of this specialization
      > is illegal. The solution is to define the class specialization.
      >[/color]

      Then how come this is accepted, and works properly?

      template <> void blah<int>::ugh( int x)
      {
      cout << typeid(feh).nam e() << endl;
      }
      [color=blue]
      > Dave
      >
      >
      >
      > ---
      > Outgoing mail is certified Virus Free.
      > Checked by AVG anti-virus system (http://www.grisoft.com).
      > Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003[/color]

      Comment

      • David B. Held

        #4
        Re: Possible g++ bug in template parsing

        "Rennie deGraaf" <rendeg@my-dejanews.com> wrote in message
        news:7cad4b2a.0 310251135.5672a b45@posting.goo gle.com...[color=blue]
        > [...]
        > Then how come this is accepted, and works properly?
        >
        > template <> void blah<int>::ugh( int x)
        > {
        > cout << typeid(feh).nam e() << endl;
        > }[/color]

        Because the compiler is broken? Usually, when something
        unexpectedly works, something else (that should) doesn't.
        Try this, for instance, define the specialization *after* this
        definition, and see what the compiler says.

        Dave



        ---
        Outgoing mail is certified Virus Free.
        Checked by AVG anti-virus system (http://www.grisoft.com).
        Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


        Comment

        Working...