Strange FUNCTOR inconsistency

Discussion in 'C++' started by RCS, Sep 8, 2003.

  1. RCS

    RCS Guest

    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
     
    RCS, Sep 8, 2003
    #1
    1. Advertising

  2. On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <> 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::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.
     
    Alf P. Steinbach, Sep 8, 2003
    #2
    1. Advertising

  3. RCS

    fabio Guest

    RCS wrote:

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

    >
    > "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?
     
    fabio, Sep 8, 2003
    #3
  4. RCS

    RCS Guest

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


    "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
     
    RCS, Sep 8, 2003
    #4
  5. RCS

    tom_usenet Guest

    On Mon, 8 Sep 2003 11:03:53 +0200, "RCS" <> wrote:

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

    >
    >"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
     
    tom_usenet, Sep 8, 2003
    #5
  6. RCS

    RCS Guest

    That was the missing piece!

    Thanks a lot!

    RCS

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

    > >
    > >"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
     
    RCS, Sep 8, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chandra Shekhar Kumar

    functor object in template class

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    5
    Views:
    1,293
    porschberg
    Jun 26, 2003
  2. Sean
    Replies:
    2
    Views:
    486
  3. Gert  Van den Eynde

    Abstract base class + Functor

    Gert Van den Eynde, Sep 23, 2003, in forum: C++
    Replies:
    6
    Views:
    615
    tom_usenet
    Sep 23, 2003
  4. Ned Balzer
    Replies:
    4
    Views:
    2,486
    Ned Balzer
    Aug 3, 2006
  5. andrew cooke
    Replies:
    1
    Views:
    283
    Chris Rebert
    Apr 20, 2011
Loading...

Share This Page