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 8707193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:48:37+00:00 2026-06-13T03:48:37+00:00

In C++ there is a standard library function called cout , which lets me

  • 0

In C++ there is a standard library function called cout, which lets me send text to the console. I am sure you know that.

#include <iostream>
using std::cout;
cout << "Some Text " << 15 << " Other Text";

To do a linebreak at the end, I need to use endl.

cout << "Some Text " << 15 << " Other Text" << endl;

How can I write a function named coutl which behaves like cout but also adds a likebreak? I want to use the same syntax that cout uses, especially the << operator.

coutl << "Some Text " << 15 << " Other Text"; // coutl should add a linebreak
  • 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-06-13T03:48:38+00:00Added an answer on June 13, 2026 at 3:48 am

    By creating a little proxy object which adds << endl in its destructor:

    class AddEndl
    {
    public:
      AddEndl(std::ostream& os) : _holder(new Holder(os)) {}
    
      template <class T>
      friend std::ostream& operator<< (const AddEndl& l, const T& t)
      {
        return (l._holder->_os) << t;
      }
    
    private:
      struct Holder {
        Holder (std::ostream& os) : _os(os) {}
        ~Holder () { _os << std::endl; }
    
        std::ostream& _os;
      };
    
      mutable std::shared_ptr<Holder> _holder;
    }
    

    Then you need a function so that you will get a temporary:

    AddEndl wrap(std::ostream& os)
    {
      return AddEndl(os);
    }
    

    This should then work:

    wrap(std::cout) << "Hello";
    

    UPDATE:

    I move the destructor which adds a std::endl to an inner object owned by a std::shared_ptr<> so that the example doesn’t depend on Copy Elision anymore.

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

Sidebar

Related Questions

Is there a standard library function to check that a string S is of
I am wondering if there is a standard library function in Python which will
Is there a standard library function which will set a minimum value to a
Is there a standard Java library that handles common file operations such as moving/copying
Is there a Fortran standard library (I keep hearing about something called ISO_C_BINDING, but
Is there a C++ Standard Template Library class that provides efficient string concatenation functionality,
Is there a standard mechanism or known library that will convert .png images to
I have a shared library say foo.so which contains a function called initialize() .
Is there a cross-platform library function that would collapse a multiline string into a
Is there python function in standard library like def cond(condition, true, false): if condition:

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.