find() of std::set

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

    find() of std::set

    Hello Newsgroup

    I have a question about the find function of std::set.
    When I have a "std::set<int*> ", why can't I call the find() function with an
    "const int*"? I know that my key type is different from the type of the
    parameter I give to the find function but can't the find() function be
    written in a way where this would work? Normally, "int*" can be compared
    with "const int*" without problems...


    And as a follow-up question: What would you do in a function like this:

    void MyClass::myFunc (const LargeObject* input)
    {
    // ...
    // m_LargeObjectSe t is a member variable of the class MyClass and has
    the type "std::set<Large Object*>". How would you write the following line?
    const_const? Copy the large object?
    // m_LargeObjectSe t.find(input);
    // ...
    }

    The function already exists and I would have to rewrite quite a lot of code
    when I would remove the "const" of the parameter type.
    Do you have any suggestions?


    Thanks & greetings
    Chris


  • Rolf Magnus

    #2
    Re: find() of std::set

    Christian Meier wrote:
    Hello Newsgroup
    >
    I have a question about the find function of std::set.
    When I have a "std::set<int*> ", why can't I call the find() function with
    an "const int*"? I know that
    Because:
    my key type is different from the type of the parameter I give to the find
    function but can't the find() function be written in a way where
    this would work?
    I guess it could in theory. But it would mean that the class std::set would
    have to be specialized just for this specific case.
    Normally, "int*" can be compared with "const int*" without problems...
    And as a follow-up question: What would you do in a function like this:
    >
    void MyClass::myFunc (const LargeObject* input)
    {
    // ...
    // m_LargeObjectSe t is a member variable of the class MyClass and has
    the type "std::set<Large Object*>". How would you write the following line?
    const_const? Copy the large object?
    // m_LargeObjectSe t.find(input);
    m_LargeObjectSe t.find(const_ca st<LargeObject* >(input));
    // ...
    }

    Comment

    • Joe Smith

      #3
      Re: find() of std::set


      Rolf Magnus wrote:
      Christian Meier wrote:
      >
      >Hello Newsgroup
      >>
      >I have a question about the find function of std::set.
      >When I have a "std::set<int*> ", why can't I call the find() function with
      >an "const int*"? I know that
      >
      Because:
      >
      >my key type is different from the type of the parameter I give to the
      >find
      >function but can't the find() function be written in a way where
      >this would work?
      >
      I guess it could in theory. But it would mean that the class std::set
      would
      have to be specialized just for this specific case.
      >
      I think in general, a "const T*" can be compaired with a "T*", no?

      Unfortuneately, the way the language works "const T&" with T=int* is not the
      same
      as const int*&.

      What is the reasoning behind that?


      Comment

      • Bo Persson

        #4
        Re: find() of std::set

        Joe Smith wrote:
        Rolf Magnus wrote:
        >Christian Meier wrote:
        >>
        >>Hello Newsgroup
        >>>
        >>I have a question about the find function of std::set.
        >>When I have a "std::set<int*> ", why can't I call the find()
        >>function with an "const int*"? I know that
        >>
        >Because:
        >>
        >>my key type is different from the type of the parameter I give to
        >>the find
        >>function but can't the find() function be written in a way where
        >>this would work?
        >>
        >I guess it could in theory. But it would mean that the class
        >std::set would
        >have to be specialized just for this specific case.
        >>
        >
        I think in general, a "const T*" can be compaired with a "T*", no?
        >
        Unfortuneately, the way the language works "const T&" with T=int*
        is not the same
        as const int*&.
        >
        What is the reasoning behind that?
        The reason is that "const T" should mean the same as "T const". So you
        get "int* const&".

        The type system works differently than #define macros - it is not a
        straight text substitution.


        Bo Persson


        Comment

        • Joe Smith

          #5
          Re: find() of std::set


          "Bo Persson" <bop@gmb.dkwrot e in message
          news:6nrqmdFi1o tU1@mid.individ ual.net...
          Joe Smith wrote:
          >Rolf Magnus wrote:
          >>Christian Meier wrote:
          >>>
          >>>Hello Newsgroup
          >>>>
          >>>I have a question about the find function of std::set.
          >>>When I have a "std::set<int*> ", why can't I call the find()
          >>>function with an "const int*"? I know that
          >>>
          >>Because:
          >>>
          >>>my key type is different from the type of the parameter I give to
          >>>the find
          >>>function but can't the find() function be written in a way where
          >>>this would work?
          >>>
          >>I guess it could in theory. But it would mean that the class
          >>std::set would
          >>have to be specialized just for this specific case.
          >>>
          >>
          >I think in general, a "const T*" can be compaired with a "T*", no?
          >>
          >Unfortuneately , the way the language works "const T&" with T=int*
          >is not the same
          >as const int*&.
          >>
          >What is the reasoning behind that?
          >
          The reason is that "const T" should mean the same as "T const". So you get
          "int* const&".
          Right. Doh!

          Obviously a "reference to a constant pointer to an integer" (quite the mouth
          full) is the right way for the compiler to interpret that.



          Comment

          • James Kanze

            #6
            Re: find() of std::set

            On Nov 10, 3:26 pm, "Christian Meier" <chris@no_spam. comwrote:
            I have a question about the find function of std::set.
            When I have a "std::set<int*> ", why can't I call the find()
            function with an "const int*"? I know that my key type is
            different from the type of the parameter I give to the find
            function but can't the find() function be written in a way
            where this would work? Normally, "int*" can be compared with
            "const int*" without problems...
            The problem is that std::set<>::fin d() takes the key by
            reference, and a reference to an int* cannot be initialized with
            an int const*&. (If you could, then you could modify a const
            object without a const cast.)
            And as a follow-up question: What would you do in a function
            like this:
            void MyClass::myFunc (const LargeObject* input)
            {
                // ...
                // m_LargeObjectSe t is a member variable of the class MyClass andhas
            the type "std::set<Large Object*>". How would you write the following line?
            const_const? Copy the large object?
                // m_LargeObjectSe t.find(input);
                // ...
            }
            The function already exists and I would have to rewrite quite
            a lot of code when I would remove the "const" of the parameter
            type. Do you have any suggestions?
            The real question is what your std::set should really contain.
            Perhaps it would be more appropriate for it to contain MyType
            const*, rather than just MyType*. Not knowing what the role of
            the set is, I can't say. Most of the time, if the set contains
            pointers, you want to specialize the comparison to be based on
            the pointed to object. In which case, the pointer had better be
            to const.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • James Kanze

              #7
              Re: find() of std::set

              On Nov 10, 4:08 pm, Rolf Magnus <ramag...@t-online.dewrote:
              Christian Meier wrote:
              I have a question about the find function of std::set. When
              I have a "std::set<int*> ", why can't I call the find()
              function with an "const int*"? I know that  
              Because:
              my key type is different from the type of the parameter I
              give to the find function but can't the find() function be
              written in a way where this would work?
              I guess it could in theory. But it would mean that the class
              std::set would have to be specialized just for this specific
              case.
              Not at all. All it would require is that set<>::find() take
              its argument by value, and not by reference. Which would have
              been the better design to begin with.

              --
              James Kanze (GABI Software) email:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • James Kanze

                #8
                Re: find() of std::set

                On Nov 10, 7:48 pm, "Joe Smith" <unknown_kev_.. .@hotmail.comwr ote:
                Rolf Magnus wrote:
                Christian Meier wrote:
                Unfortuneately, the way the language works "const T&" with
                T=int* is not the same as const int*&.
                What is the reasoning behind that?
                Because it would break const:

                void
                f( int const* p )
                {
                int*& r = p ;
                *r = 42 ;
                }

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                Working...