Creating a stringstream in a parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anongroupaccount@googlemail.com

    #1

    Creating a stringstream in a parameter

    I have a base class that takes a string to its constructor. I want to
    construct a concrete class that does this:

    class concrete: public base
    {
    public:
    concrete() : base( /* create a stringstream here and convert to
    string */ ){}
    };

    However, I can't get it to work. I can pass this:

    ostringstream(" String").str()

    but not this:

    (ostringstream( "String") << 3).str()

    or this:

    ((ostringstream )(ostringstream ("String") << 3)).str()

    which is what I want to do (as I need to build the string on the fly.

    What do I do?

  • Thomas Jakob

    #2
    Re: Creating a stringstream in a parameter

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    anongroupaccoun t@googlemail.co m wrote:[color=blue]
    > I have a base class that takes a string to its constructor. I want to
    > construct a concrete class that does this:
    >
    > class concrete: public base
    > {
    > public:
    > concrete() : base( /* create a stringstream here and convert to
    > string */ ){}
    > };
    >
    > However, I can't get it to work. I can pass this:
    >
    > ostringstream(" String").str()
    >
    > but not this:
    >
    > (ostringstream( "String") << 3).str()
    >
    > or this:
    >
    > ((ostringstream )(ostringstream ("String") << 3)).str()
    >
    > which is what I want to do (as I need to build the string on the fly.
    >
    > What do I do?
    >[/color]


    I would prefer to use a template function to convert any type to a
    string using a stringstream:

    template <class StringType, class SourceType>
    inline StringType string_cast(con st SourceType& value)
    {
    std::basic_ostr ingstream<typen ame StringType::val ue_type> stream;
    stream << value;
    return stream.str();
    }

    Using it:
    "String" + string_cast<std ::string>(123)

    Or using boost:
    "String" + boost::lexical_ cast<std::strin g>(123)

    Hope this helps.

    - --
    Greetings, Thomas Jakob
    quicix (at) gmail (dot) com
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (MingW32)

    iD8DBQFDunV7KzG u1S9H/nsRAms7AKC5JUXj 9SNn6T1NNOia7H4 gqVOWLACgtoea
    hkFrjX7TsuZT39L Nxs+DirY=
    =RYlx
    -----END PGP SIGNATURE-----

    Comment

    • John Carson

      #3
      Re: Creating a stringstream in a parameter

      <anongroupaccou nt@googlemail.c om> wrote in message
      news:1136290385 .549960.296830@ z14g2000cwz.goo glegroups.com[color=blue]
      > I have a base class that takes a string to its constructor. I want to
      > construct a concrete class that does this:
      >
      > class concrete: public base
      > {
      > public:
      > concrete() : base( /* create a stringstream here and convert to
      > string */ ){}
      > };
      >
      > However, I can't get it to work. I can pass this:
      >
      > ostringstream(" String").str()
      >
      > but not this:
      >
      > (ostringstream( "String") << 3).str()
      >
      > or this:
      >
      > ((ostringstream )(ostringstream ("String") << 3)).str()
      >
      > which is what I want to do (as I need to build the string on the fly.
      >
      > What do I do?[/color]

      You need to cast to a reference to ostringstream, not to an ostringstream
      object:

      ((ostringstream &)(ostringstrea m("String")<<3) ).str()

      It would also be nicer if you used a static_cast:

      static_cast<ost ringstream&>(os tringstream("St ring")<<3).str( )


      --
      John Carson


      Comment

      • Dervish

        #4
        Re: Creating a stringstream in a parameter

        >static_cast<os tringstream&>(o stringstream("S tring")<<3).str ()

        To make this work I have added seekp:
        (static_cast<os tringstream&>(( ostringstream(" String").seekp( 0,ios::end)
        << 3))).str()

        Comment

        • John Carson

          #5
          Re: Creating a stringstream in a parameter

          "Dervish" <DAkhlynin@yand ex.ru> wrote in message
          news:1136293978 .774962.193450@ g44g2000cwa.goo glegroups.com[color=blue][color=green]
          >> static_cast<ost ringstream&>(os tringstream("St ring")<<3).str( )[/color]
          >
          > To make this work I have added seekp:
          > (static_cast<os tringstream&>(( ostringstream(" String").seekp( 0,ios::end)
          > << 3))).str()[/color]

          What "works" depends on what the objective is, on which the OP was not
          explicit. If the objective is to add a 3 to the end of "String", then you
          are right. You can, incidentally, make do with less brackets:

          static_cast<ost ringstream&>(os tringstream("St ring").seekp(0, ios::end)<<
          3).str()


          --
          John Carson


          Comment

          • Earl Purple

            #6
            Re: Creating a stringstream in a parameter


            John Carson wrote:[color=blue]
            > "Dervish" <DAkhlynin@yand ex.ru> wrote in message
            > news:1136293978 .774962.193450@ g44g2000cwa.goo glegroups.com[color=green][color=darkred]
            > >> static_cast<ost ringstream&>(os tringstream("St ring")<<3).str( )[/color]
            > >
            > > To make this work I have added seekp:
            > > (static_cast<os tringstream&>(( ostringstream(" String").seekp( 0,ios::end)
            > > << 3))).str()[/color]
            >
            > What "works" depends on what the objective is, on which the OP was not
            > explicit. If the objective is to add a 3 to the end of "String", then you
            > are right. You can, incidentally, make do with less brackets:
            >
            > static_cast<ost ringstream&>(os tringstream("St ring").seekp(0, ios::end)<<
            > 3).str()
            >[/color]

            Do not cast away what the compiler tells you is not allowed. The
            compiler is telling you that for a reason.

            ostringstream(" String") creates a temporary. Now you can call str() on
            it because that is a const function. But you cannot call << 3 because
            that is a non-const function - it modifies the temporary. And that is
            undefined behaviour.

            No need for a one-liner. Use an inline function. One was supplied a few
            posts above.

            Comment

            • John Carson

              #7
              Re: Creating a stringstream in a parameter

              "Earl Purple" <earlpurple@gma il.com> wrote in message
              news:1136309414 .300233.221280@ f14g2000cwb.goo glegroups.com[color=blue]
              > John Carson wrote:[color=green]
              >> "Dervish" <DAkhlynin@yand ex.ru> wrote in message
              >> news:1136293978 .774962.193450@ g44g2000cwa.goo glegroups.com[color=darkred]
              >>>> static_cast<ost ringstream&>(os tringstream("St ring")<<3).str( )
              >>>
              >>> To make this work I have added seekp:
              >>> (static_cast<os tringstream&>(( ostringstream(" String").seekp( 0,ios::end)
              >>> << 3))).str()[/color]
              >>
              >> What "works" depends on what the objective is, on which the OP was
              >> not explicit. If the objective is to add a 3 to the end of "String",
              >> then you are right. You can, incidentally, make do with less
              >> brackets:
              >>
              >> static_cast<ost ringstream&>(os tringstream("St ring").seekp(0, ios::end)<<
              >> 3).str()
              >>[/color]
              >
              > Do not cast away what the compiler tells you is not allowed. The
              > compiler is telling you that for a reason.
              >
              > ostringstream(" String") creates a temporary. Now you can call str() on
              > it because that is a const function. But you cannot call << 3 because
              > that is a non-const function - it modifies the temporary. And that is
              > undefined behaviour.[/color]

              The reason for the non-compilation is not because a temporary has been
              modified. Modifying a temporary is perfectly legal. You may note that the
              following argument compiles without warnings:

              string("String" ).append(" extra bit")

              The compilation fails because operator<< is inherited from ostringstream's
              base class of basic_ostream<c har> and thus operator<< returns a reference to
              this base class of basic_ostream<c har> rather than a reference to
              ostringstream. basic_ostream<c har> has no str() member function and that is
              the error message given --- nothing about modifying a temporary. The cast is
              a downcast, made with knowledge that the object referenced is indeed an
              ostringstream and not merely a basic_ostream<c har>.

              --
              John Carson


              Comment

              Working...