Strange FUNCTOR inconsistency

R

RCS

I have this Functor:

typedef std::vector < std::string > String_vec;

typedef std::vector < String_vec > StrVec_vec;

class Compare : public std::unary_function<String_vec, void>
{
public:
StrVec_vec myData;
Compare(StrVec_vec const& data) : myData(data) {}

void operator() (const String_vec& data) const
{
StrVec_vec datax = myData;
std::pair<StrVec_vec::iterator, StrVec_vec::iterator > range =
std::equal_range(datax.begin(), datax.myData.end(), data,
DataCompare());
}
};

This one above compiles fine (Visual Studio 7.0), but the assignment of
myData to datax seems a waste, and I would rather have it this way:

.....

void operator() (const String_vec& data) const
{
std::pair<StrVec_vec::iterator, StrVec_vec::iterator > range =
std::equal_range(myData.begin(), myData.myData.end(), data,
DataCompare()); // I access myData directly in the loop instead of first
assigning it to datax and then using datax in the loop
}

......

BUT this last variant does not compile ??????????????????? (Error message
"..... cannot convert parameter 1 from 'const std::vector<_Ty,
_Ax>::const_iterator' to 'std::vector<_Ty, _Ax>::_Tptr .............")

Anyone got a clue why myData variable is OK as an assignment to a temporary,
and this temporary is OK in the loop, while myData is not OK in the loop?
This does not make sense to me!

RCS
 
A

Alf P. Steinbach

I have this Functor:

typedef std::vector < std::string > String_vec;

typedef std::vector < String_vec > StrVec_vec;

class Compare : public std::unary_function<String_vec, void>
{
public:
StrVec_vec myData;
Compare(StrVec_vec const& data) : myData(data) {}

void operator() (const String_vec& data) const
{
StrVec_vec datax = myData;
std::pair<StrVec_vec::iterator, StrVec_vec::iterator > range =
std::equal_range(datax.begin(), datax.myData.end(), data, DataCompare());

Here 'datax' is of type 'StrVec_vec'.

'StrVec_vec' does not have a member 'myData', as far as I can see.
}
};

This one above compiles fine (Visual Studio 7.0)

It should absolutely not compile, and in fact does not compile.


...
This does not make sense to me!

It simply does not make sense at all.
 
F

fabio

RCS said:
"It SHOULD not compile" is fine with me, but the fact is it DOES compile
(with my version of the compiler).......

Whatever, what IS interesting to know (for me) is why this SHOULD not
compile, and if anybody has got another way of accomplishing what I'm
trying to do, I would be very grateful!

RCS

could you explain us what would you like doing?
 
R

RCS

Alf P. Steinbach said:
DataCompare());

Here 'datax' is of type 'StrVec_vec'.

'StrVec_vec' does not have a member 'myData', as far as I can see.


It should absolutely not compile, and in fact does not compile.

"It SHOULD not compile" is fine with me, but the fact is it DOES compile
(with my version of the compiler).......

Whatever, what IS interesting to know (for me) is why this SHOULD not
compile, and if anybody has got another way of accomplishing what I'm trying
to do, I would be very grateful!

RCS
 
T

tom_usenet

"It SHOULD not compile" is fine with me, but the fact is it DOES compile
(with my version of the compiler).......

Whatever, what IS interesting to know (for me) is why this SHOULD not
compile, and if anybody has got another way of accomplishing what I'm trying
to do, I would be very grateful!

The code you posted can't really compile. Are you sure you posted the
actual code? Anyway, I think this is what you are looking for:

void operator()(const String_vec& data) const
{
std::pair<StrVec_vec::const_iterator, StrVec_vec::const_iterator>
range = std::equal_range(myData.begin(), myData.end(), data);
}

The problem was caused by the fact that in a const member function,
data members are const, and hence myData.begin() returns a
const_iterator.

Tom
 
R

RCS

That was the missing piece!

Thanks a lot!

RCS

tom_usenet said:
The code you posted can't really compile. Are you sure you posted the
actual code? Anyway, I think this is what you are looking for:

void operator()(const String_vec& data) const
{
std::pair<StrVec_vec::const_iterator, StrVec_vec::const_iterator>
range = std::equal_range(myData.begin(), myData.end(), data);
}

The problem was caused by the fact that in a const member function,
data members are const, and hence myData.begin() returns a
const_iterator.

Tom
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top