Compiler errors: set with comparison function

C

clark.coleman

I have a header file that compiles fine on Visual Studio .NET 2005 but
has big trouble on x86/Linux using gcc-4.1.1

I need to make sets of elements of type "op_t", which needs a complex
comparison function. Near the top of my header, I include the ehader
that defines "op_t", then I make this ordering function:

class LessOp {
public:
bool operator()(const op_t Opnd1, const op_t Opnd2) const {
if (Opnd1.type != Opnd2.type)
return (Opnd1.type < Opnd2.type);
switch (Opnd1.type) {

etc.
etc.

The first time I make reference to "LessOp" later in the header, I get
the compile errors from gcc but not Visual Studio. The reference is a
public member function of a class, declared as:

set<const op_t, LessOp>::iterator GetFirstLiveIn(void); // First
LiveIn ...

This is line 180 of SMPDataFlowAnalysis.h, as referenced in this gcc
error message:


/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/ext/
new_allocator.h: In instantiation of '__gnu_cxx::new_allocator<const
op_t>':
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/
bits/allocator.h:83: instantiated from 'std::allocator<const op_t>'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/
bits/stl_set.h:110: instantiated from 'std::set<const op_t, LessOp,
std::allocator<const op_t> >'
SMPDataFlowAnalysis.h:180: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/ext/
new_allocator.h:78: error: 'const _Tp*
__gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp =
const op_t]' cannot be overloaded
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/ext/
new_allocator.h:75: error: with '_Tp*
__gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const
op_t]'

I thought maybe I needed to make the parameters to LessOp's operator
be by reference, so I changed the declaration to be:

class LessOp {
public:
bool operator()(const op_t &Opnd1, const op_t &Opnd2) const {

This produces the exact same error message from gcc, and still no
errors or warnings from Visual Studio.

I have found numerous similar problems in this group and via search
engine, but almost all of them were the opposite problem of people not
using "const" whereas I used "const" all over the place.

Any help is appreciated.
 
B

Barry

I have a header file that compiles fine on Visual Studio .NET 2005 but
has big trouble on x86/Linux using gcc-4.1.1
set<const op_t, LessOp>::iterator GetFirstLiveIn(void); // First
^^^^^
set< op_t, LessOp>....

set requires the type to be assignable.

As the implementation in vc2005, the allocator has a specialization of
allocator (actually Allocator_base as base class) for const type, which
simply removes the constness. Which I guess is an extension (not so
beautiful one).
 
C

clark.coleman

OK. It seems the problem that I had was that I switched between "const
everywhere" and "const nowhere" and kept getting mysterious errors
either way. The solution was to leave "const" on the arguments to the
comparison function and remove it everywhere else. That finally got
the errors to go away on both compilers.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top