Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:07:53+00:00 2026-05-28T01:07:53+00:00

I’m thinking about a problem which has some similarity with perfect forwarding, but where

  • 0

I’m thinking about a problem which has some similarity with perfect forwarding, but where the function argument is not passed to a called function, but returned. This is why I call it “perfect pass-through”.

The problem is the following:

Say we have a function which takes an object by reference (and possibly some extra arguments), modifies that object, and returns the modified object. The best-known example of such functions are probably operator<< and operator>> for iostreams.

Let’s use iostreams as example, because it allows to nicely show what I’m after. For example, one thing which one would sometimes like to do is:

std::string s = (std::ostringstream() << foo << bar << baz).str();

Of course that doesn’t work, for two reasons:

  • std::ostringstream() is an rvalue, but operator<< takes an lvalue as first argument

  • operator<< returns an ostream& (well, at least for the standard ones actually a basic_ostream<CharT, Traits>& where CharT and Traits are deduced from the first argument).

So let’s assume we want to design the insertion operator so that the above works (you obviously can’t do that for the existing operators, but you can do that for your own classes). Obviously the solution should have the following traits:

  • The first argument can accept either an lvalue or an rvalue.

  • The return type should be the same type as passed in. But of course it should still only accept ostreams (i.e. classes derived from an instantiation of basic_ostream).

While in this specific use case it isn’t needed, I want to add a third requirement:

  • If the first argument is an rvalue, so is the returned value, otherwise an lvalue is returned.

This extra rule is so that you can move-construct from an rvalue passed through the function (I don’t know if C++11 streams are move constructible, but it is intended to be a more general scheme, with the stream just as handy example).

It is obvious that in C++03, those requirements could not all be met. However, in C++11, we have rvalue references, which should make this possible.

Here’s my try on this:

#include <iostream>
#include <sstream>
#include <string>

template<typename Ostream> struct is_ostream
{
  typedef typename std::remove_reference<Ostream>::type candidate;
  typedef typename candidate::char_type char_type;
  typedef typename candidate::traits_type traits_type;
  typedef std::basic_ostream<char_type, traits_type> basic_ostream;
  static const bool value = std::is_base_of<basic_ostream, candidate>::value;
};

class SomeType {};

template<typename Ostream>
 typename std::enable_if<is_ostream<Ostream>::value, Ostream&&>::type
  operator<<(Ostream&& is, SomeType const& x)
{
  is << "SomeType";
  return std::forward<Ostream>(is);
}

int main()
{
  SomeType t;

  std::string s = (std::ostringstream() << t).str();

  std::cout << s << std::endl;
}

It indeed compiles with gcc (using the option -std=c++0x), and when run outputs SomeType as expected. However, it is a quite complicated machinery to get there.

Therefore my questions:

  1. Is the output operator as I wrote it indeed working as expected (and fulfilling all the requirements I gave)? Or could it fail in unexpected ways or have other unexpected consequences? Especially: Is my use of std::forward correct here?

  2. Is there an easier way to meet the requirements?

  3. Assuming this is indeed correct and the simplest way to do it: Do you consider it a good idea to do that, or would you advice against it (both specifically for stream output as in my example, and as a more general scheme for passing objects through functions)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:07:53+00:00Added an answer on May 28, 2026 at 1:07 am

    std::ostringstream() is an rvalue, but operator<< takes an lvalue
    as first argument

    There’s a generic inserter to take rvalue streams, but it returns a basic_ostream<charT, traits>&:

    template <class charT, class traits, class T>
      basic_ostream<charT, traits>&
      operator<<(basic_ostream<charT, traits>&& os, const T& x);
    

    To work correctly for your example it would have to return the derived type of the stream (std::ostringstram).

    1. Is the output operator as I wrote it indeed working as expected (and fulfilling all the requirements I gave)? Or could it fail in
      unexpected ways or have other unexpected consequences? Especially: Is
      my use of std::forward correct here?

    Your code looks correct to me.

    1. Is there an easier way to meet the requirements?

    Your code looks similar to the code I wrote to solve this problem (see below).

    1. Assuming this is indeed correct and the simplest way to do it: Do you consider it a good idea to do that, or would you advice against it
      (both specifically for stream output as in my example, and as a more
      general scheme for passing objects through functions)?

    The idiom looks fine to me.

    In libc++ I defined the rvalue ostream inserter like the following (as an extension). I made an attempt to get it standardized but it was late and the committee was understandably not in the mood for more thrashing:

    template <class _Stream, class _Tp>
    inline
    typename enable_if
    <
        !is_lvalue_reference<_Stream>::value &&
        is_base_of<ios_base, _Stream>::value,
        _Stream&&
    >::type
    operator<<(_Stream&& __os, const _Tp& __x)
    {
        __os << __x;
        return std::move(__os);
    }
    

    Unlike yours, this only accepts rvalue streams. But like yours, it returns the concrete derived type, and so works with your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.