Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Style question

Style question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++dockertutorial
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #1

    I have a C++ function that takes as argument an iterator and needs to increment it without passing the end of the container. I can write it as: variant A:

    bool func (std::string::iterator& ptr, std::string& container)
    {
    //...
    if (ptr != container.end())
    //do stuff
    }

    and call it as:

    func (s.begin(), s);

    or I could write it as variant B:

    bool func (std::string::interator& ptr, std::string::const_iterator& limit)
    {
    //...
    if (ptr != limit)
    //do stuff
    }

    and call it as:

    func (s.begin(), s.end());

    (the example assumes the container is a string but that's not important) Which one would you favor?

    Mircea

    G C B 3 Replies Last reply
    0
    • Mircea NeacsuM Mircea Neacsu

      I have a C++ function that takes as argument an iterator and needs to increment it without passing the end of the container. I can write it as: variant A:

      bool func (std::string::iterator& ptr, std::string& container)
      {
      //...
      if (ptr != container.end())
      //do stuff
      }

      and call it as:

      func (s.begin(), s);

      or I could write it as variant B:

      bool func (std::string::interator& ptr, std::string::const_iterator& limit)
      {
      //...
      if (ptr != limit)
      //do stuff
      }

      and call it as:

      func (s.begin(), s.end());

      (the example assumes the container is a string but that's not important) Which one would you favor?

      Mircea

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      If the function modified the contents of the container I would probably pass it the container reference, otherwise I would prefer the iterators (or even a std::span).

      Mircea NeacsuM 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        I have a C++ function that takes as argument an iterator and needs to increment it without passing the end of the container. I can write it as: variant A:

        bool func (std::string::iterator& ptr, std::string& container)
        {
        //...
        if (ptr != container.end())
        //do stuff
        }

        and call it as:

        func (s.begin(), s);

        or I could write it as variant B:

        bool func (std::string::interator& ptr, std::string::const_iterator& limit)
        {
        //...
        if (ptr != limit)
        //do stuff
        }

        and call it as:

        func (s.begin(), s.end());

        (the example assumes the container is a string but that's not important) Which one would you favor?

        Mircea

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        The second one: give to the function the strict necessary for its task.

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        Mircea NeacsuM 1 Reply Last reply
        0
        • G Graham Breach

          If the function modified the contents of the container I would probably pass it the container reference, otherwise I would prefer the iterators (or even a std::span).

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          Good points, thank you! However std::span is C++20 and I want to keep the code C++14.

          Mircea

          1 Reply Last reply
          0
          • C CPallini

            The second one: give to the function the strict necessary for its task.

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            Mircea NeacsuM Offline
            Mircea NeacsuM Offline
            Mircea Neacsu
            wrote on last edited by
            #5

            Yep, my thoughts exactly. Thank you!

            Mircea

            C 1 Reply Last reply
            0
            • Mircea NeacsuM Mircea Neacsu

              Yep, my thoughts exactly. Thank you!

              Mircea

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              "great minds..." :laugh: You are welcome.

              "In testa che avete, Signor di Ceprano?" -- Rigoletto

              1 Reply Last reply
              0
              • Mircea NeacsuM Mircea Neacsu

                I have a C++ function that takes as argument an iterator and needs to increment it without passing the end of the container. I can write it as: variant A:

                bool func (std::string::iterator& ptr, std::string& container)
                {
                //...
                if (ptr != container.end())
                //do stuff
                }

                and call it as:

                func (s.begin(), s);

                or I could write it as variant B:

                bool func (std::string::interator& ptr, std::string::const_iterator& limit)
                {
                //...
                if (ptr != limit)
                //do stuff
                }

                and call it as:

                func (s.begin(), s.end());

                (the example assumes the container is a string but that's not important) Which one would you favor?

                Mircea

                B Offline
                B Offline
                BernardIE5317
                wrote on last edited by
                #7

                I have a more or less strict rule i.e. to wit in particular to be specific namely things that act like pointers are passed via copy constructor not via reference since raw pointers are fundamental type objects so take up little space and a reference is a pointer anyway. I agree w/ the chap who suggested passing only what the function requires i.e. first, last iterators. Further at point of call the code is easier to understand its purpose as it passes only what the function requires also it just looks better and is easier to understand as fewer ideas/concepts are involved namely the one idea/concept "iterator" rather than the two ideas/concepts "iterator and container". Was there not a recent article in a recent CP newsletter discussing this very thing i.e. minimizing the number of ideas/concepts needed to understand any code?

                Mircea NeacsuM 1 Reply Last reply
                0
                • B BernardIE5317

                  I have a more or less strict rule i.e. to wit in particular to be specific namely things that act like pointers are passed via copy constructor not via reference since raw pointers are fundamental type objects so take up little space and a reference is a pointer anyway. I agree w/ the chap who suggested passing only what the function requires i.e. first, last iterators. Further at point of call the code is easier to understand its purpose as it passes only what the function requires also it just looks better and is easier to understand as fewer ideas/concepts are involved namely the one idea/concept "iterator" rather than the two ideas/concepts "iterator and container". Was there not a recent article in a recent CP newsletter discussing this very thing i.e. minimizing the number of ideas/concepts needed to understand any code?

                  Mircea NeacsuM Offline
                  Mircea NeacsuM Offline
                  Mircea Neacsu
                  wrote on last edited by
                  #8

                  meagreProgrammer wrote:

                  things that act like pointers are passed via copy constructor not via reference

                  Except that in my case the iterator needs to be changed by the function, hence I have to pass a reference.

                  Mircea

                  B 1 Reply Last reply
                  0
                  • Mircea NeacsuM Mircea Neacsu

                    meagreProgrammer wrote:

                    things that act like pointers are passed via copy constructor not via reference

                    Except that in my case the iterator needs to be changed by the function, hence I have to pass a reference.

                    Mircea

                    B Offline
                    B Offline
                    BernardIE5317
                    wrote on last edited by
                    #9

                    Would it be too lengthy or tedious an explanation to explain the need to modify the iterator. I find such a need quite unusual and do not recall ever having to perform same.

                    Mircea NeacsuM 1 Reply Last reply
                    0
                    • B BernardIE5317

                      Would it be too lengthy or tedious an explanation to explain the need to modify the iterator. I find such a need quite unusual and do not recall ever having to perform same.

                      Mircea NeacsuM Offline
                      Mircea NeacsuM Offline
                      Mircea Neacsu
                      wrote on last edited by
                      #10

                      No problem: the iterator is a pointer in a UTF-8 encoded string and the function (called next) has to advance to the next code point (1, 2, 3 or 4 char). If iterator is at end of string it doesn't advance. Although a very simple function, I had a number of design decisions to make: - How should I deal with improperly encoded UTF-8 strings? I decided to return false if the string is not properly encoded. - Should I just leave out the boundary check and just document it? I decided against as it would have been unsafe. And the last one I was asking about: For limit check, should I just pass the string or the end iterator.

                      Mircea

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups