Strange FUNCTOR inconsistency

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RCS

    Strange FUNCTOR inconsistency

    I have this Functor:

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

    typedef std::vector < String_vec > StrVec_vec;

    class Compare : public std::unary_func tion<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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
    std::equal_rang e(datax.begin() , datax.myData.en d(), 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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
    std::equal_rang e(myData.begin( ), myData.myData.e nd(), 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_ite rator' to 'std::vector<_T y, _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


  • Alf P. Steinbach

    #2
    Re: Strange FUNCTOR inconsistency

    On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <rcs333@online. no> wrote:
    [color=blue]
    >I have this Functor:
    >
    >typedef std::vector < std::string > String_vec;
    >
    >typedef std::vector < String_vec > StrVec_vec;
    >
    >class Compare : public std::unary_func tion<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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
    > std::equal_rang e(datax.begin() , datax.myData.en d(), data, DataCompare());[/color]

    Here 'datax' is of type 'StrVec_vec'.

    'StrVec_vec' does not have a member 'myData', as far as I can see.
    [color=blue]
    >}
    >};
    >
    >This one above compiles fine (Visual Studio 7.0)[/color]

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


    [color=blue]
    >...
    >This does not make sense to me![/color]

    It simply does not make sense at all.

    Comment

    • RCS

      #3
      Re: Strange FUNCTOR inconsistency


      "Alf P. Steinbach" <alfps@start.no > skrev i melding
      news:3f5c2acb.3 57370078@News.C IS.DFN.DE...[color=blue]
      > On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <rcs333@online. no> wrote:
      >[color=green]
      > >I have this Functor:
      > >
      > >typedef std::vector < std::string > String_vec;
      > >
      > >typedef std::vector < String_vec > StrVec_vec;
      > >
      > >class Compare : public std::unary_func tion<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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
      > > std::equal_rang e(datax.begin() , datax.myData.en d(), data,[/color][/color]
      DataCompare());[color=blue]
      >
      > Here 'datax' is of type 'StrVec_vec'.
      >
      > 'StrVec_vec' does not have a member 'myData', as far as I can see.
      >[color=green]
      > >}
      > >};
      > >
      > >This one above compiles fine (Visual Studio 7.0)[/color]
      >
      > It should absolutely not compile, and in fact does not compile.[/color]

      "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


      Comment

      • tom_usenet

        #4
        Re: Strange FUNCTOR inconsistency

        On Mon, 8 Sep 2003 11:03:53 +0200, "RCS" <rcs333@online. no> wrote:
        [color=blue]
        >
        >"Alf P. Steinbach" <alfps@start.no > skrev i melding
        >news:3f5c2acb. 357370078@News. CIS.DFN.DE...[color=green]
        >> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <rcs333@online. no> wrote:
        >>[color=darkred]
        >> >I have this Functor:
        >> >
        >> >typedef std::vector < std::string > String_vec;
        >> >
        >> >typedef std::vector < String_vec > StrVec_vec;
        >> >
        >> >class Compare : public std::unary_func tion<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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
        >> > std::equal_rang e(datax.begin() , datax.myData.en d(), data,[/color][/color]
        >DataCompare()) ;[color=green]
        >>
        >> Here 'datax' is of type 'StrVec_vec'.
        >>
        >> 'StrVec_vec' does not have a member 'myData', as far as I can see.
        >>[color=darkred]
        >> >}
        >> >};
        >> >
        >> >This one above compiles fine (Visual Studio 7.0)[/color]
        >>
        >> It should absolutely not compile, and in fact does not compile.[/color]
        >
        >"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![/color]

        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()(cons t String_vec& data) const
        {
        std::pair<StrVe c_vec::const_it erator, StrVec_vec::con st_iterator>
        range = std::equal_rang e(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

        Comment

        • fabio

          #5
          Re: Strange FUNCTOR inconsistency

          RCS wrote:
          [color=blue]
          >
          > "Alf P. Steinbach" <alfps@start.no > skrev i melding
          > news:3f5c2acb.3 57370078@News.C IS.DFN.DE...[color=green]
          >> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <rcs333@online. no> wrote:
          >>[color=darkred]
          >> >I have this Functor:
          >> >
          >> >typedef std::vector < std::string > String_vec;
          >> >
          >> >typedef std::vector < String_vec > StrVec_vec;
          >> >
          >> >class Compare : public std::unary_func tion<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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
          >> > std::equal_rang e(datax.begin() , datax.myData.en d(), data,[/color][/color]
          > DataCompare());[color=green]
          >>
          >> Here 'datax' is of type 'StrVec_vec'.
          >>
          >> 'StrVec_vec' does not have a member 'myData', as far as I can see.
          >>[color=darkred]
          >> >}
          >> >};
          >> >
          >> >This one above compiles fine (Visual Studio 7.0)[/color]
          >>
          >> It should absolutely not compile, and in fact does not compile.[/color]
          >
          > "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[/color]

          could you explain us what would you like doing?

          Comment

          • RCS

            #6
            Re: Strange FUNCTOR inconsistency

            That was the missing piece!

            Thanks a lot!

            RCS

            "tom_usenet " <tom_usenet@hot mail.com> skrev i melding
            news:3f5c78ce.2 47772375@news.e asynet.co.uk...[color=blue]
            > On Mon, 8 Sep 2003 11:03:53 +0200, "RCS" <rcs333@online. no> wrote:
            >[color=green]
            > >
            > >"Alf P. Steinbach" <alfps@start.no > skrev i melding
            > >news:3f5c2acb. 357370078@News. CIS.DFN.DE...[color=darkred]
            > >> On Mon, 8 Sep 2003 09:07:33 +0200, "RCS" <rcs333@online. no> wrote:
            > >>
            > >> >I have this Functor:
            > >> >
            > >> >typedef std::vector < std::string > String_vec;
            > >> >
            > >> >typedef std::vector < String_vec > StrVec_vec;
            > >> >
            > >> >class Compare : public std::unary_func tion<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<StrVe c_vec::iterator , StrVec_vec::ite rator > range =
            > >> > std::equal_rang e(datax.begin() , datax.myData.en d(), data,[/color]
            > >DataCompare()) ;[color=darkred]
            > >>
            > >> 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.[/color]
            > >
            > >"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[/color][/color]
            trying[color=blue][color=green]
            > >to do, I would be very grateful![/color]
            >
            > 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()(cons t String_vec& data) const
            > {
            > std::pair<StrVe c_vec::const_it erator, StrVec_vec::con st_iterator>
            > range = std::equal_rang e(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[/color]


            Comment

            Working...