bug in gcc?

Discussion in 'C++' started by Timon Gehr, Dec 23, 2011.

  1. Timon Gehr

    Timon Gehr Guest

    g++ fails to compile the following code snippet:
    ---
    struct B{int foo;};

    template<class T> int foo(T x){
    int y = x.foo<1>(0);
    return y;
    }

    int main(){
    int c = foo(B());
    }
    ---
    ---
    bug.cpp: In function ‘int foo(T) [with T = B]’:
    bug.cpp:10:17: instantiated from here
    bug.cpp:5:20: error: ‘foo’ is not a member template function
    ---

    The code compiles if the template function is renamed from 'foo' to
    'bar'.

    Is this a bug?
     
    Timon Gehr, Dec 23, 2011
    #1
    1. Advertising

  2. On 23.12.2011 12:04, Timon Gehr wrote:
    > g++ fails to compile the following code snippet:
    > ---
    > struct B{int foo;};
    >
    > template<class T> int foo(T x){
    > int y = x.foo<1>(0);
    > return y;
    > }
    >
    > int main(){
    > int c = foo(B());
    > }
    > ---
    > ---
    > bug.cpp: In function ‘int foo(T) [with T = B]’:
    > bug.cpp:10:17: instantiated from here
    > bug.cpp:5:20: error: ‘foo’ is not a member template function
    > ---
    >
    > The code compiles if the template function is renamed from 'foo' to
    > 'bar'.
    >
    > Is this a bug?


    If it's true that it compiles with the simple renaming, then yes.

    However, your posting is rather unclear.

    Why on Earth are you showing the non-problematic code instead of the
    code where there is possibly a problem?

    Given the lack of logic in the posting, I think it's more likely that
    you're confused about this.

    I.e., a PEBKAC problem.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Dec 23, 2011
    #2
    1. Advertising

  3. Timon Gehr

    Timon Gehr Guest

    On Dec 23, 12:28 pm, "Alf P. Steinbach" <alf.p.steinbach
    > wrote:
    > On 23.12.2011 12:04, Timon Gehr wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > g++ fails to compile the following code snippet:
    > > ---
    > > struct B{int foo;};

    >
    > > template<class T>  int foo(T x){
    > >      int y = x.foo<1>(0);
    > >      return y;
    > > }

    >
    > > int main(){
    > >      int c = foo(B());
    > > }
    > > ---
    > > ---
    > > bug.cpp: In function ‘int foo(T) [with T = B]’:
    > > bug.cpp:10:17:   instantiated from here
    > > bug.cpp:5:20: error: ‘foo’ is not a member template function
    > > ---

    >
    > > The code compiles if the template function is renamed from 'foo' to
    > > 'bar'.

    >
    > > Is this a bug?

    >
    > If it's true that it compiles with the simple renaming, then yes.
    >
    > However, your posting is rather unclear.


    I disagree.

    >
    > Why on Earth are you showing the non-problematic code instead of the
    > code where there is possibly a problem?
    >


    I am showing the problematic code. I think it is valid C++. GCC
    rejects it. The code where 'foo' is replaced by 'bar' is not
    problematic: It is valid C++ and GCC compiles it.

    > Given the lack of logic in the posting, I think it's more likely that
    > you're confused about this.


    I don't know, but I think it is possible that you are confused because
    x.foo<1>(0) looks like a template function instantiation. I thought
    that was the compiler's issue too, but I was wrong: I have managed to
    reduce the code to a handy one-liner that does not contain it.

    ---
    template<class T> bool foo(T x){return x.foo<1;}
    ---
    ---
    bug.cpp: In function ‘bool foo(T)’:
    bug.cpp:1:42: error: parse error in template argument list
    ---

    Again, renaming the template function makes the code compile.

    >
    > I.e., a PEBKAC problem.
    >
    > Cheers & hth.,
    >
    > - Alf


    Thank you.
     
    Timon Gehr, Dec 23, 2011
    #3
  4. Timon Gehr

    Rui Maciel Guest

    Alf P. Steinbach wrote:

    > If it's true that it compiles with the simple renaming, then yes.
    >
    > However, your posting is rather unclear.
    >
    > Why on Earth are you showing the non-problematic code instead of the
    > code where there is possibly a problem?


    The example code, when compiled with GCC 4.6.1, generates the exact same
    error message which was presented by Timon. Therefore, it does appear to
    fit the definition of problematic code.


    Rui Maciel
     
    Rui Maciel, Dec 23, 2011
    #4
  5. Timon Gehr

    Timon Gehr Guest

  6. Timon Gehr

    jacob navia Guest

    Le 23/12/11 12:04, Timon Gehr a écrit :
    > struct B{int foo;};
    >
    > template<class T> int foo(T x){
    > int y = x.foo<1>(0);
    > return y;
    > }
    >
    > int main(){
    > int c = foo(B());
    > }


    It is specified in the Gnu standard paragraph 6.6.6:

    "Note: Gnu is not foo. Thou shalt not use foo in your programs"

    Confirmed for

    gcc version 4.2.1 (Apple Inc. build 5664)

    in my Mac.
     
    jacob navia, Dec 23, 2011
    #6
  7. Timon Gehr

    Rui Maciel Guest

    Marco Minutoli wrote:

    > To me this looks like the problematic code :).
    >
    > When we have the template function named bar the line:
    > int y = x.foo<1>(0);
    >
    > is interpreted as:
    > int y = (x.foo<1)>(0);
    >
    > What is not clear to me is why naming foo the template function change
    > this behavior.


    Probably it's an implicit template instantiation thing. If the definition
    of class T is unavailable and the template function invokes x.foo<1>(0),
    then maybe it is assumed that class T has a template member function whose
    identifier is foo, instead of the code representing a convoluted set of
    comparissons between ints.


    Rui Maciel
     
    Rui Maciel, Dec 23, 2011
    #7
  8. Timon Gehr

    Timon Gehr Guest

    On Dec 23, 3:14 pm, Rui Maciel <> wrote:
    > Marco Minutoli wrote:
    > > To me this looks like the problematic code :).

    >
    > > When we have the template function named bar the line:
    > > int y = x.foo<1>(0);

    >
    > > is interpreted as:
    > > int y = (x.foo<1)>(0);

    >
    > > What is not clear to me is why naming foo the template function change
    > > this behavior.

    >
    > Probably it's an implicit template instantiation thing.  If the definition
    > of class T is unavailable and the template function invokes x.foo<1>(0),
    > then maybe it is assumed that class T has a template member function whose
    > identifier is foo, instead of the code representing a convoluted set of
    > comparissons between ints.
    >
    > Rui Maciel


    I don't think that holds:

    ---
    template<class T> bool foo(T x){return x.foo<1;} // not
    convoluted, does not compile
    ---
    template<class T> bool bar(T x){return x.foo<1>(0);} // convoluted,
    compiles as 2 comparisons
    struct B{int foo;}; int main(){bar(B());} //
    ---
     
    Timon Gehr, Dec 23, 2011
    #8
  9. Timon Gehr wrote:

    > g++ fails to compile the following code snippet:
    > ---
    > struct B{int foo;};
    >
    > template<class T> int foo(T x){
    > int y = x.foo<1>(0);
    > return y;
    > }
    >
    > int main(){
    > int c = foo(B());
    > }
    > ---
    > ---
    > bug.cpp: In function ‘int foo(T) [with T = B]’:
    > bug.cpp:10:17: instantiated from here
    > bug.cpp:5:20: error: ‘foo’ is not a member template function
    > ---
    >
    > The code compiles if the template function is renamed from 'foo' to
    > 'bar'.
    >
    > Is this a bug?


    GCC behavior is incorrect. It should accept both.
     
    Johannes Schaub, Dec 24, 2011
    #9
  10. Timon Gehr

    Miles Bader Guest

    Johannes Schaub <> writes:
    >> The code compiles if the template function is renamed from 'foo' to
    >> 'bar'.
    >>
    >> Is this a bug?

    >
    > GCC behavior is incorrect. It should accept both.


    Hmm, I notice clang gives the same error ....

    -miles

    --
    I'd rather be consing.
     
    Miles Bader, Dec 25, 2011
    #10
    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:
    8
    Views:
    462
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    679
    Kevin P. Fleming
    Nov 6, 2003
  3. Replies:
    5
    Views:
    388
    Nathan Addy
    Sep 17, 2005
  4. ashnin

    GCC 3.4.3 and GCC 4.1.2

    ashnin, Jul 7, 2008, in forum: C++
    Replies:
    1
    Views:
    585
    Michael DOUBEZ
    Jul 7, 2008
  5. kas
    Replies:
    1
    Views:
    340
    red floyd
    Apr 22, 2010
Loading...

Share This Page