public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
@ 2003-03-31 10:04 darrenr
  0 siblings, 0 replies; 6+ messages in thread
From: darrenr @ 2003-03-31 10:04 UTC (permalink / raw)
  To: gcc-gnats


>Number:         10273
>Category:       c++
>Synopsis:       g++ fails to compile sort() with a template operator that takes a pointer
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Mar 31 09:56:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     darrenr@optimation.com.au
>Release:        unknown-1.0
>Organization:
>Environment:
Linux linux 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux
>Description:
% g++ --version
g++ (GCC) 3.3 20030331 (prerelease)
% uname -a
Linux linux 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux

The following code does not compile with the above version with g++.  The error message seen is as follows:

% g++ foo.cpp
foo.cpp: In function `void std::doit()':
foo.cpp:31: error: type/value mismatch at argument 1 in template parameter list
   for `template<std::ClassName*<anonymous> > struct std::xLess'
foo.cpp:31: error:   expected a constant of type `std::ClassName*', got `
   std::ClassName*'

If the code is wrong then the error message is wrong.
However, if I add this line:

typedef ClassName * ClassNamePtr;

and change all references of "ClassName *" to "ClassNamePtr", I now get an error about "x being private
to ClassName (seems wrong also?) and finally making x public gets rid of all errors.  This is just a code sample to show the problem, unfortunately the I'm not able to do the typedef thing in the application for other reasons :-(

#include <new>
#include <string>
#include <vector>

namespace std {

class ClassName {
public:
        ClassName(int n) { x = n; }
private:
        int x;
};

template <class ClassName *> struct xLess : binary_function<ClassName *,ClassName *,bool>
{
        bool operator()(const ClassName *& T1, const ClassName *& T2) const
        {
                return T1->x < T2->x;
        }
};


void doit()
{
        vector<ClassName *> a;

        a.push_back(new ClassName(1));
        a.push_back(new ClassName(2));
        a.push_back(new ClassName(5));

        sort(a.begin(), a.end(), xLess<ClassName *>());
}

};

int main(int argc, char *argv[])
{
        std::doit();
        return 0;
}
>How-To-Repeat:
See description.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
@ 2003-04-01  2:36 Wolfgang Bangerth
  0 siblings, 0 replies; 6+ messages in thread
From: Wolfgang Bangerth @ 2003-04-01  2:36 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/10273; it has been noted by GNATS.

From: Wolfgang Bangerth <bangerth@ices.utexas.edu>
To: "Darren Reed (OSE)" <darrenr@optimation.com.au>
Cc: gcc-bugs@gcc.gnu.org, <gcc-gnats@gcc.gnu.org>
Subject: RE: c++/10273: g++ fails to compile sort() with a template operator
 that takes a pointer
Date: Mon, 31 Mar 2003 20:26:00 -0600 (CST)

 > >   x.cc: In function `void std::doit()':
 > >   x.cc:31: type/value mismatch at argument 1 in template parameter list for `
 > >    template<std::ClassName*<anonymous> > struct std::xLess'
 > >   x.cc:31:   expected a constant of type `std::ClassName*', got `std::ClassName*'
 > >
 > > I think the message says it quite clearly: first the type/value mismatch,
 > > then in addition that it expected a value and got a type. What more do you
 > > want?
 > 
 > Ok, so you know how to parse that message better than I do.  To me it says
 > "expected type 'A', got 'A'" (where A is "std::ClassName*") and hence it
 > does not make any sense to me. 
 
 No, read it, it says
     expected a constant [i.e. a value] of type 'X', got 'X' [which is a type]
 
 The situation is the same as if you had written
     template <int N> class X{};
 and then used it as
     X<int> x;
 That won't work.
 
 
 > I'm the programmer, so because I don't
 > intend for there to be a value there, I don't see a value there. 
 
 If you intend there to be a type, then you should declare the template as 
 taking a type, rather than a value.
 
 
 > The error
 > doesn't tell me that using * implies a value is being used in a template
 > where a type should be.
 
 What you wrote was 
     template <class ClassName *>  struct X {};
 which, since ClassName is the name of an existing class, is equivalent to
     template <ClassName *> struct X {};
 (that is just the same as declaring variables as  "struct X x;" or "X x;"
 is equivalent in C++.)
 
 
 > Just for the sake of comparison, I tried compiling the same piece of code
 > with Visual Studio .Net and SUNWspro CC.  Both of those compilers (rightly
 > or wrongly, I don't know) complain about the code when it comes to parsing
 > the template statement.  In my situation, this type of verbosity helps.
 > [...verbosity of other compilers deleted...]
 
 These compilers simply don't realize that
     template <class C *> struct X {};
 and
     template <C *>
 
 W.
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth             email:            bangerth@ticam.utexas.edu
                               www: http://www.ticam.utexas.edu/~bangerth/
 
 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
@ 2003-04-01  0:26 Darren Reed (OSE)
  0 siblings, 0 replies; 6+ messages in thread
From: Darren Reed (OSE) @ 2003-04-01  0:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/10273; it has been noted by GNATS.

From: "Darren Reed \(OSE\)" <darrenr@optimation.com.au>
To: "Wolfgang Bangerth" <bangerth@ices.utexas.edu>
Cc: <gcc-bugs@gcc.gnu.org>, <gcc-gnats@gcc.gnu.org>
Subject: RE: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
Date: Tue, 1 Apr 2003 10:10:46 +1000

 > > Is it unreasonable to ask for a better error message in this case ?
 > > Or perhaps better checking of templates ?  (This would probably cause
 > > a more accurate error message to be output.)
 >
 > I guess in this case, the error message is as clear as it can be: the
 > declaration of the template is legal (you parameterize it on a value, not
 > on a type as you had expected) so there is nothing the compiler could have
 > told you there. Then you tried to use the template with a type instead of
 > the required value and the compiler told you
 >   x.cc: In function `void std::doit()':
 >   x.cc:31: type/value mismatch at argument 1 in template parameter list for `
 >    template<std::ClassName*<anonymous> > struct std::xLess'
 >   x.cc:31:   expected a constant of type `std::ClassName*', got `std::ClassName*'
 >
 > I think the message says it quite clearly: first the type/value mismatch,
 > then in addition that it expected a value and got a type. What more do you
 > want?
 
 Ok, so you know how to parse that message better than I do.  To me it says
 "expected type 'A', got 'A'" (where A is "std::ClassName*") and hence it
 does not make any sense to me.  I'm the programmer, so because I don't
 intend for there to be a value there, I don't see a value there.  The error
 doesn't tell me that using * implies a value is being used in a template
 where a type should be.
 
 Just for the sake of comparison, I tried compiling the same piece of code
 with Visual Studio .Net and SUNWspro CC.  Both of those compilers (rightly
 or wrongly, I don't know) complain about the code when it comes to parsing
 the template statement.  In my situation, this type of verbosity helps.
 
 d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(17) : error C2504: 'binary_function' : base class
 undefined
         d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(23) : see reference to class template
 instantiation 'std::xLess<>' being compiled
 d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(17) : error C2143: syntax error : missing ','
 before '<'
 d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(34) : error C2065: 'sort' : undeclared identifier
 d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(34) : error C2975: 'std::xLess' : invalid
 template argument for 'function-parameter', constant expression expected
         d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(23) : see declaration of 'std::xLess'
 d:\Documents and Settings\darrenr\My Documents\Visual Studio Projects\foo\foo2.cpp(34) : error C2514: 'std::xLess<__formal>' : class
 has no constructors
         with
         [
             __formal=0
         ]
 
 And the compiler from Sun (SUNWspro CC) has this to say:
 "/tmp/foo.cpp", line 14: Error: "," expected instead of "*".
 "/tmp/foo.cpp", line 14: Error:  Expected "class" or "typename" before "*" .
 "/tmp/foo.cpp", line 14: Error: binary_function is not defined.
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 14: Error: "," expected instead of "<".
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 14: Error: binary_function is not defined.
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 14: Error: "," expected instead of "<".
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 14: Error: binary_function is not defined.
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 14: Error: "," expected instead of "<".
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 31: Error: The type "std::xLess<std::ClassName*>" is incomplete.
 "/tmp/foo.cpp", line 14: Error: binary_function is not defined.
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 "/tmp/foo.cpp", line 14: Error: "," expected instead of "<".
 "/tmp/foo.cpp", line 31:     Where: While specializing "std::xLess<std::ClassName*>".
 "/tmp/foo.cpp", line 31:     Where: Specialized in non-template code.
 
 Darren
 
 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
@ 2003-03-31 23:26 Darren Reed (OSE)
  0 siblings, 0 replies; 6+ messages in thread
From: Darren Reed (OSE) @ 2003-03-31 23:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/10273; it has been noted by GNATS.

From: "Darren Reed \(OSE\)" <darrenr@optimation.com.au>
To: <bangerth@dealii.org>, <darrenr@optimation.com.au>, <gcc-bugs@gcc.gnu.org>,
        <gcc-prs@gcc.gnu.org>, <nobody@gcc.gnu.org>, <gcc-gnats@gcc.gnu.org>
Cc:  
Subject: RE: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
Date: Tue, 1 Apr 2003 09:06:35 +1000

 Is it unreasonable to ask for a better error message in this case ?
 Or perhaps better checking of templates ?  (This would probably cause
 a more accurate error message to be output.)
 
 Darren
 
 > -----Original Message-----
 > From: bangerth@dealii.org [mailto:bangerth@dealii.org]
 > Sent: Tuesday, 1 April 2003 2:00 AM
 > To: darrenr@optimation.com.au; gcc-bugs@gcc.gnu.org;
 > gcc-prs@gcc.gnu.org; nobody@gcc.gnu.org
 > Subject: Re: c++/10273: g++ fails to compile sort() with a template
 > operator that takes a pointer
 > 
 > 
 > Synopsis: g++ fails to compile sort() with a template operator that takes a pointer
 > 
 > State-Changed-From-To: open->closed
 > State-Changed-By: bangerth
 > State-Changed-When: Mon Mar 31 15:59:41 2003
 > State-Changed-Why:
 >     Not a bug, but for a different reason than what Gaby
 >     suspected. This
 >       template <class ClassName *> struct xLess
 >     is indeed a valid template declaration, since a class named
 >     "ClassName" has been declared previously, but it is a
 >     template with a value-parameter, not a type-parameter.
 >     
 >     Once you change that, change some pointers to values
 >     and make ClassName::x public, the code compiles.
 >     
 >     W.
 > 
 > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10273
 > 
 > 
 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
@ 2003-03-31 16:10 bangerth
  0 siblings, 0 replies; 6+ messages in thread
From: bangerth @ 2003-03-31 16:10 UTC (permalink / raw)
  To: darrenr, gcc-bugs, gcc-prs, nobody

Synopsis: g++ fails to compile sort() with a template operator that takes a pointer

State-Changed-From-To: open->closed
State-Changed-By: bangerth
State-Changed-When: Mon Mar 31 15:59:41 2003
State-Changed-Why:
    Not a bug, but for a different reason than what Gaby
    suspected. This
      template <class ClassName *> struct xLess
    is indeed a valid template declaration, since a class named
    "ClassName" has been declared previously, but it is a
    template with a value-parameter, not a type-parameter.
    
    Once you change that, change some pointers to values
    and make ClassName::x public, the code compiles.
    
    W.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10273


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
@ 2003-03-31 10:11 Gabriel Dos Reis
  0 siblings, 0 replies; 6+ messages in thread
From: Gabriel Dos Reis @ 2003-03-31 10:11 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/10273; it has been noted by GNATS.

From: Gabriel Dos Reis <gdr@integrable-solutions.net>
To: darrenr@optimation.com.au
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c++/10273: g++ fails to compile sort() with a template operator that takes a pointer
Date: 31 Mar 2003 12:00:28 +0200

 darrenr@optimation.com.au writes:
 
 | template <class ClassName *> struct xLess :
             ^^^^^^^^^^^^^^^^^^
 
 That is an invalid template parameter declaration.
 
 -- Gaby


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-04-01  2:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-31 10:04 c++/10273: g++ fails to compile sort() with a template operator that takes a pointer darrenr
2003-03-31 10:11 Gabriel Dos Reis
2003-03-31 16:10 bangerth
2003-03-31 23:26 Darren Reed (OSE)
2003-04-01  0:26 Darren Reed (OSE)
2003-04-01  2:36 Wolfgang Bangerth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).