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

  • Home
  • SEARCH
  • 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 955867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:27:16+00:00 2026-05-16T00:27:16+00:00

[ EDIT – I long since forgot this was here until I got the

  • 0

[EDIT – I long since forgot this was here until I got the 2,500 views “notable question”. Since people are viewing – there is useful information about overloads in the accepted answer, but specifically checking for std::endl is even worse than I realized at the time, and definitely the wrong thing.

Basically, the effect of std::endl is to output \n to the stream then flush with std::flush. That’s irrespective of platform, including Windows where the end-of-line is really “\r\n”. The endl manipulator doesn’t abstract away platform differences WRT line ends, C++ handles that the same way that C does – by translating \n to “\r\n” (for text mode, not binary) later on. I thought C++ was doing something different, an assumption so strong I never even questioned it for 2 decades, but I was wrong.

I don’t remember details, but it’s possible to define your own streams anyway, and provide alternative output (and translation) of whatever characters are streamed in. All manipulators should work as expected, before your custom stream code sees the resulting output characters. So to provide special end-of-line behaviour, watch for the \n there (which is still before the text-file end-of-line translation).
]

It’s hackish, I know, but I recently needed to implement a stream class which would act mostly like a standard stream, but which would detect the std::endl manipulator and special-case it’s behaviour. My first attempt at a particular method implementation was…

mystream& mystream::operator<< (std::basic_ostream<char>& (*p) (std::basic_ostream<char>&))
{
  if (p == &std::endl)
  {
    //  Handle special case
  }
  else
  {
    m_Underlying_Stream << p;
  }

  return *this;
}

The trouble with this is that the compiler doesn’t know which overload of std::endl I’m referring to. I resolved it as follows…

mystream& mystream::operator<< (std::basic_ostream<char>& (*p) (std::basic_ostream<char>&))
{
  typedef std::basic_ostream<char>& (*ENDL_T) (std::basic_ostream<char>&);

  const ENDL_T l_ENDL (&std::endl);

  if (p == l_ENDL)
  {
    //  Handle special case
  }
  else
  {
    m_Underlying_Stream << p;
  }

  return *this;
}

That is the compiler can resolve the overload in the context of an initialisation (and for assignment too, as another experiment proved), but not for operator==.

The compiler in question is MinGW GCC 4.4.0, but I don’t think this is likely to be a compiler issue.

I had a look around and found this question…

How to get the address of an overloaded member function?

If my code has a const issue, I don’t know where the missing const needs to go. I can’t see any other obvious type issue.

I have some vague ideas about number-of-steps issues WRT overloading or implicit casting, but nothing concrete. So – can anyone explain clearly what is wrong with my first example, why the second version fixes it, and how I can safely indicate which overload I mean when taking the address of a function.

BTW – I can guess some people won’t like me testing directly for the address of std::endl, and will point out that this is fragile – e.g. someone could have their own manipulator which calls std::endl which I wouldn’t spot. In general this is true, but in this special case, the hack saves a lot of time and the nastiness just doesn’t matter.

  • 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-16T00:27:17+00:00Added an answer on May 16, 2026 at 12:27 am

    The use of an overloaded function name, (or the name of a function template which behaves like a set of overloaded functions) without arguments (such as in an “address of” expression) is only allowed in a limited set of contexts where the context can be used to uniquely determine the particular overload required.

    This is specified in 13.4 of the standard (ISO/IEC 14882:2003) [over.over]. Included are an initializer for an object or reference or in an explicit conversion. This gives you a number of options.

    E.g. explicit conversion:

    typedef std::ostream& (*ManipPtr)(std::ostream&);
    
    mystream& mystream::operator<<(ManipPtr p)
    {
        if (p == static_cast<ManipPtr>(&std::endl))
        {
            // ...
    

    Directly initializing a pointer:

    typedef std::ostream& (*ManipPtr)(std::ostream&);
    
    mystream& mystream::operator<<(ManipPtr p)
    {
        const ManipPtr pEndl = &std::endl;
    
        if (p == pEndl)
        {
            // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a rake task (alt link) that shows how… May 16, 2026 at 1:51 pm
  • Editorial Team
    Editorial Team added an answer There are plenty of license management systems out there for… May 16, 2026 at 1:51 pm
  • Editorial Team
    Editorial Team added an answer ExpandEnvironmentString May 16, 2026 at 1:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

EDIT: had to retag this, because, it's rather a Sweave / R question since
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT: upgraded this question to MVC 2.0 With asp.net MVC 2.0 is there an
Edit: This is technically a 2 part question. I've chosen the best answer that
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Edit: Just wanted to make the question I have more clear. I pretty much

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.