Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange FUNCTOR inconsistency

Reply
Thread Tools

Strange FUNCTOR inconsistency

 
 
RCS
Guest
Posts: n/a
 
      09-08-2003
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:air<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:air<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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-08-2003
On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <(E-Mail Removed)> wrote:

>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:air<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.

 
Reply With Quote
 
 
 
 
fabio
Guest
Posts: n/a
 
      09-08-2003
RCS wrote:

>
> "Alf P. Steinbach" <(E-Mail Removed)> skrev i melding
> news:(E-Mail Removed)...
>> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <(E-Mail Removed)> wrote:
>>
>> >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:air<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.

>
> "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?
 
Reply With Quote
 
RCS
Guest
Posts: n/a
 
      09-08-2003

"Alf P. Steinbach" <(E-Mail Removed)> skrev i melding
news:(E-Mail Removed)...
> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <(E-Mail Removed)> wrote:
>
> >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:air<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.


"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


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      09-08-2003
On Mon, 8 Sep 2003 11:03:53 +0200, "RCS" <(E-Mail Removed)> wrote:

>
>"Alf P. Steinbach" <(E-Mail Removed)> skrev i melding
>news:(E-Mail Removed)...
>> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <(E-Mail Removed)> wrote:
>>
>> >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:air<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.

>
>"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:air<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
 
Reply With Quote
 
RCS
Guest
Posts: n/a
 
      09-08-2003
That was the missing piece!

Thanks a lot!

RCS

"tom_usenet" <(E-Mail Removed)> skrev i melding
news:(E-Mail Removed)...
> On Mon, 8 Sep 2003 11:03:53 +0200, "RCS" <(E-Mail Removed)> wrote:
>
> >
> >"Alf P. Steinbach" <(E-Mail Removed)> skrev i melding
> >news:(E-Mail Removed)...
> >> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <(E-Mail Removed)> wrote:
> >>
> >> >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:air<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.

> >
> >"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:air<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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My stupidity / strange inconsistency overriding class methods andrew cooke Python 1 04-20-2011 12:45 AM
strange inconsistency referencing controls in Formview itemtemplate Ned Balzer ASP .Net 4 08-03-2006 05:14 PM
Abstract base class + Functor Gert Van den Eynde C++ 6 09-23-2003 05:11 PM
stl for_each makes two additional copies of functor Sean C++ 2 07-01-2003 07:19 AM
functor object in template class Chandra Shekhar Kumar C++ 5 06-26-2003 04:55 AM



Advertisments