adding a bool to a string

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

    #1

    adding a bool to a string

    Hi All,

    I'm trying to print boolean values and after I searched the entire web,
    I couldn't find anything that does this =/ The code that I ended up
    using is as follows:

    string boolString(Obje ct* obj_, size_t maxOutput_){
    string string_;

    for (size_t i=0; i<maxOutput_; i++){

    if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
    if (obj_->boolean(i) == true)
    string_ += "1";
    else
    string_ += "0";
    }
    }

    return string_;
    }

    Is there any way to simplify this code?

  • Rolf Magnus

    #2
    Re: adding a bool to a string

    aaragon wrote:
    Hi All,
    >
    I'm trying to print boolean values and after I searched the entire web,
    I couldn't find anything that does this =/ The code that I ended up using
    is as follows:
    >
    string boolString(Obje ct* obj_, size_t maxOutput_){
    string string_;
    >
    for (size_t i=0; i<maxOutput_; i++){
    >
    if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
    What is the above line supposed to do? What does obj_->boolean(i) return?
    Note that sizeof is always computed at compile time.
    if (obj_->boolean(i) == true)
    string_ += "1";
    else
    string_ += "0";
    }
    }
    >
    return string_;
    }
    >
    Is there any way to simplify this code?
    string boolString(Obje ct* obj_, size_t maxOutput_){
    stringstream stream_;

    for (size_t i=0; i<maxOutput_; i++)
    // depending on the return type of boolean(), you can remove the cast
    stream_ << bool(obj_->boolean(i));

    return stream_.str();
    }

    Comment

    • Victor Bazarov

      #3
      Re: adding a bool to a string

      aaragon wrote:
      I'm trying to print boolean values and after I searched the entire
      web, I couldn't find anything that does this =/ The code that I
      ended up using is as follows:
      >
      string boolString(Obje ct* obj_, size_t maxOutput_){
      string string_;
      >
      for (size_t i=0; i<maxOutput_; i++){
      >
      if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
      Why do you need to check the size? What does it give you?
      if (obj_->boolean(i) == true)
      string_ += "1";
      else
      string_ += "0";
      This could be written

      string_ += (obj_->boolean(i) ? '1' : '0');

      which I find a bit simpler.

      Also, consider that you call 'boolean' member twice here (once to
      check the size and the other to actually get the value); if the
      function has side effects, calling it twice may not be what you
      want.
      }
      }
      >
      return string_;
      }
      >
      Is there any way to simplify this code?
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • aaragon

        #4
        Re: adding a bool to a string


        Victor Bazarov wrote:
        aaragon wrote:
        I'm trying to print boolean values and after I searched the entire
        web, I couldn't find anything that does this =/ The code that I
        ended up using is as follows:

        string boolString(Obje ct* obj_, size_t maxOutput_){
        string string_;

        for (size_t i=0; i<maxOutput_; i++){

        if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
        >
        Why do you need to check the size? What does it give you?
        I need to check the size because the function may not return a bool but
        an integer for example.
        >
        if (obj_->boolean(i) == true)
        string_ += "1";
        else
        string_ += "0";
        >
        This could be written
        >
        string_ += (obj_->boolean(i) ? '1' : '0');
        >
        which I find a bit simpler.
        >
        Also, consider that you call 'boolean' member twice here (once to
        check the size and the other to actually get the value); if the
        function has side effects, calling it twice may not be what you
        want.
        >
        }
        }

        return string_;
        }

        Is there any way to simplify this code?
        >
        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask

        Comment

        • aaragon

          #5
          Re: adding a bool to a string


          Rolf Magnus wrote:
          aaragon wrote:
          >
          Hi All,

          I'm trying to print boolean values and after I searched the entire web,
          I couldn't find anything that does this =/ The code that I ended up using
          is as follows:

          string boolString(Obje ct* obj_, size_t maxOutput_){
          string string_;

          for (size_t i=0; i<maxOutput_; i++){

          if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
          >
          What is the above line supposed to do? What does obj_->boolean(i) return?
          Note that sizeof is always computed at compile time.
          >
          if (obj_->boolean(i) == true)
          string_ += "1";
          else
          string_ += "0";
          }
          }

          return string_;
          }

          Is there any way to simplify this code?
          >
          string boolString(Obje ct* obj_, size_t maxOutput_){
          stringstream stream_;
          >
          for (size_t i=0; i<maxOutput_; i++)
          // depending on the return type of boolean(), you can remove the cast
          stream_ << bool(obj_->boolean(i));
          >
          return stream_.str();
          }
          Sometimes boolean() may return an integer, so doing a cast to a boolean
          is not a good idea. This is the reason behind the check of the size of
          whatever type boolean() returns. I thought that the sream can
          automatically convert to 1 or 0 from a boolean as

          stream_ += boolean(i);

          but this is not the case. However, if I repeat the previous with an
          integer, it works fine.

          Comment

          • Victor Bazarov

            #6
            Re: adding a bool to a string

            Rolf Magnus wrote:
            aaragon wrote:
            >
            >Hi All,
            >>
            >I'm trying to print boolean values and after I searched the entire
            >web,
            >I couldn't find anything that does this =/ The code that I ended up
            >using is as follows:
            >>
            > string boolString(Obje ct* obj_, size_t maxOutput_){
            > string string_;
            >>
            > for (size_t i=0; i<maxOutput_; i++){
            >>
            > if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
            >
            What is the above line supposed to do? What does obj_->boolean(i)
            return? Note that sizeof is always computed at compile time.
            >
            > if (obj_->boolean(i) == true)
            > string_ += "1";
            > else
            > string_ += "0";
            > }
            > }
            >>
            > return string_;
            > }
            >>
            >Is there any way to simplify this code?
            >
            string boolString(Obje ct* obj_, size_t maxOutput_){
            stringstream stream_;
            >
            for (size_t i=0; i<maxOutput_; i++)
            // depending on the return type of boolean(), you can remove the
            cast stream_ << bool(obj_->boolean(i));
            >
            return stream_.str();
            }
            Consider that some locales do not have '1' or '0' as the default
            for outputting a bool value. If the OP wanted '1' or '0', it should
            probably be explicit.

            And how is using a stringstream simpler (better) than, say,

            for (size_t i=0; i<maxOutput_; i++)
            string_ += (bool(obj_->boolean(i)) ? '1' : '0');

            return string_;

            ?

            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask


            Comment

            • Howard

              #7
              Re: adding a bool to a string


              "aaragon" <alejandro.arag on@gmail.comwro te in message
              news:1163617470 .334923.67870@k 70g2000cwa.goog legroups.com...
              >
              Victor Bazarov wrote:
              >aaragon wrote:
              I'm trying to print boolean values and after I searched the entire
              web, I couldn't find anything that does this =/ The code that I
              ended up using is as follows:
              >
              string boolString(Obje ct* obj_, size_t maxOutput_){
              string string_;
              >
              for (size_t i=0; i<maxOutput_; i++){
              >
              if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
              >>
              >Why do you need to check the size? What does it give you?
              >
              I need to check the size because the function may not return a bool but
              an integer for example.
              >
              And how do you know that a value is a bool just because it's the same size
              as one?

              Also, boolean() is your own function, so you should already know what its
              return type is. It can't sometimes return a bool and other times return an
              int. (Unless, I guess, if "boolean" isn't really a function member of obj_
              at all, but is instead a function pointer which you assign elsewhere.)

              -Howard



              Comment

              • Victor Bazarov

                #8
                Re: adding a bool to a string

                aaragon wrote:
                Victor Bazarov wrote:
                >aaragon wrote:
                >>I'm trying to print boolean values and after I searched the entire
                >>web, I couldn't find anything that does this =/ The code that I
                >>ended up using is as follows:
                >>>
                >> string boolString(Obje ct* obj_, size_t maxOutput_){
                >> string string_;
                >>>
                >> for (size_t i=0; i<maxOutput_; i++){
                >>>
                >> if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
                >>
                >Why do you need to check the size? What does it give you?
                >
                I need to check the size because the function may not return a bool
                but an integer for example.
                So? An integer is convertible to bool. Besides, sizes of 'bool' and
                'int' _can_ be the same. The check is no good. You need to check
                'typeid' to be sure.
                [..]
                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask


                Comment

                • aaragon

                  #9
                  Re: adding a bool to a string

                  >
                  Consider that some locales do not have '1' or '0' as the default
                  for outputting a bool value. If the OP wanted '1' or '0', it should
                  probably be explicit.
                  >
                  And how is using a stringstream simpler (better) than, say,
                  >
                  for (size_t i=0; i<maxOutput_; i++)
                  string_ += (bool(obj_->boolean(i)) ? '1' : '0');
                  >
                  return string_;
                  >
                  ?
                  >
                  Well, this definitely will work for booleans. But, the object I'm
                  working on, would also be a container for other values (say double,
                  integers) in which case boolean representation is no longer useful
                  (forget about the name boolean(i), it could be anything, like
                  value(i)). Therefore, the cast that you're using is not useful
                  anymore.
                  V
                  --
                  Please remove capital 'A's when replying by e-mail
                  I do not respond to top-posted replies, please don't ask

                  Comment

                  • Victor Bazarov

                    #10
                    Re: adding a bool to a string

                    Howard wrote:
                    "aaragon" <alejandro.arag on@gmail.comwro te in message
                    news:1163617470 .334923.67870@k 70g2000cwa.goog legroups.com...
                    >>
                    >Victor Bazarov wrote:
                    >>aaragon wrote:
                    >>>I'm trying to print boolean values and after I searched the entire
                    >>>web, I couldn't find anything that does this =/ The code that I
                    >>>ended up using is as follows:
                    >>>>
                    >>> string boolString(Obje ct* obj_, size_t maxOutput_){
                    >>> string string_;
                    >>>>
                    >>> for (size_t i=0; i<maxOutput_; i++){
                    >>>>
                    >>> if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
                    >>>
                    >>Why do you need to check the size? What does it give you?
                    >>
                    >I need to check the size because the function may not return a bool
                    >but an integer for example.
                    >>
                    >
                    And how do you know that a value is a bool just because it's the same
                    size as one?
                    >
                    Also, boolean() is your own function, so you should already know what
                    its return type is. It can't sometimes return a bool and other times
                    return an int. (Unless, I guess, if "boolean" isn't really a
                    function member of obj_ at all, but is instead a function pointer
                    which you assign elsewhere.)
                    If it's a function pointer, the return value type is part of the type
                    of that pointer. C++ is strict in that regard. You cannot have return
                    value types change arbitrarily during run-time.

                    V
                    --
                    Please remove capital 'A's when replying by e-mail
                    I do not respond to top-posted replies, please don't ask


                    Comment

                    • Victor Bazarov

                      #11
                      Re: adding a bool to a string

                      aaragon wrote:
                      >Consider that some locales do not have '1' or '0' as the default
                      >for outputting a bool value. If the OP wanted '1' or '0', it should
                      >probably be explicit.
                      >>
                      >And how is using a stringstream simpler (better) than, say,
                      >>
                      > for (size_t i=0; i<maxOutput_; i++)
                      > string_ += (bool(obj_->boolean(i)) ? '1' : '0');
                      >>
                      > return string_;
                      >>
                      >?
                      >>
                      >
                      Well, this definitely will work for booleans. But, the object I'm
                      working on, would also be a container for other values (say double,
                      integers) in which case boolean representation is no longer useful
                      (forget about the name boolean(i), it could be anything, like
                      value(i)). Therefore, the cast that you're using is not useful
                      anymore.
                      If for some twisted reason the 'boolean' function returns an 'int'
                      and not a 'bool', it shouldn't be used here, which means you need
                      to employ some kind of templated mechanism for rejecting classes
                      with 'boolean' members returning anything except 'bool'. See your
                      favourite C++ book on how to specialise your function on that.

                      V
                      --
                      Please remove capital 'A's when replying by e-mail
                      I do not respond to top-posted replies, please don't ask


                      Comment

                      • Howard

                        #12
                        Re: adding a bool to a string


                        "Victor Bazarov" <v.Abazarov@com Acast.netwrote in message
                        news:ejfp4p$hhq $1@news.datemas .de...
                        >Also, boolean() is your own function, so you should already know what
                        >its return type is. It can't sometimes return a bool and other times
                        >return an int. (Unless, I guess, if "boolean" isn't really a
                        >function member of obj_ at all, but is instead a function pointer
                        >which you assign elsewhere.)
                        >
                        If it's a function pointer, the return value type is part of the type
                        of that pointer. C++ is strict in that regard. You cannot have return
                        value types change arbitrarily during run-time.
                        >
                        Oh, right, of course. Thanks.

                        -Howard


                        Comment

                        • Rolf Magnus

                          #13
                          Re: adding a bool to a string

                          aaragon wrote:
                          >What is the above line supposed to do? What does obj_->boolean(i) return?
                          >Note that sizeof is always computed at compile time.
                          >>
                          if (obj_->boolean(i) == true)
                          string_ += "1";
                          else
                          string_ += "0";
                          }
                          }
                          >
                          return string_;
                          }
                          >
                          Is there any way to simplify this code?
                          >>
                          > string boolString(Obje ct* obj_, size_t maxOutput_){
                          > stringstream stream_;
                          >>
                          > for (size_t i=0; i<maxOutput_; i++)
                          > // depending on the return type of boolean(), you can remove the cast
                          > stream_ << bool(obj_->boolean(i));
                          >>
                          > return stream_.str();
                          >}
                          >
                          Sometimes boolean() may return an integer, so doing a cast to a boolean
                          is not a good idea.
                          Could you elaborate on how you think you did that? It's not possible. A
                          function's return type is fixed at compile time. Somewhere, you declared
                          the function, and part of that declaration is the return type. So if
                          boolean() returns a bool, it will always do that.
                          This is the reason behind the check of the size of whatever type boolean()
                          returns.
                          The size of bool depends on the implementation. Most often, it's the same as
                          char or the same as int.
                          I thought that the sream can automatically convert to 1 or 0 from a
                          boolean as
                          >
                          stream_ += boolean(i);
                          You need operator <<, not += to put something into a stream.

                          Comment

                          Working...