This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


>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 at optimation dot com dot 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:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]