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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:36:39+00:00 2026-05-28T19:36:39+00:00

Today I thought it would be a nice idea to overload operator<< for C

  • 0

Today I thought it would be a nice idea to overload operator<< for C style arrays:

template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, T(&a)[N])
{
    os << '{' << a[0];
    for (size_t i = 1; i < N; ++i)
    {
        os << ',' << ' ' << a[i];
    }
    os << '}';
    return os;
}

int main()
{
    int numbers[] = {2, 3, 5, 7, 11, 13, 17, 19};
    std::cout << numbers << '\n';
}

Indeed, this prints {2, 3, 5, 7, 11, 13, 17, 19} nicely. However, by providing that overload, I cannot print string literals anymore:

    std::cout << "hello world\n";

error: ambiguous overload for 'operator<<' in 'std::cout << "hello world\012"'
note: candidates are:

note: std::basic_ostream<_CharT, _Traits>::__ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _
Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_
type = std::basic_ostream<char>] <near match>

note:   no known conversion for argument 1 from 'const char [13]' to 'long int'

This is really puzzling. Why does the compiler even consider the long int overload when there is no conversion from const char[13] to long int in the first place?

Variations of this error message appear for long unsigned int, short int, short unsigned int, int, unsigned int, long long int and long long unsigned int.

(Other candidates are const void*, const char* and const _CharT*, and my own template.)


I solved the problem by providing the template for non-char types only:

template<typename T, size_t N>
typename std::enable_if<
    !std::is_same<typename std::remove_cv<T>::type, char>::value,
std::ostream&>::type operator<<(std::ostream& os, T(&a)[N])

But I’m still baffled by the question why the compiler considered the numeric types as candidates.

  • 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-28T19:36:40+00:00Added an answer on May 28, 2026 at 7:36 pm

    The first stage of overload resolution is to identify the viable functions, which are those which can accept the number of arguments provided (completely ignoring types). (See eg 13.3.2 [over.match.viable]).

    Then any needed conversions are considered to determine which is the unique best viable function.

    In this case there is no such unique best (there’s two equally good candidates).

    The error message could just tell you the two ambiguous cases. But I think they’re trying to be helpful by showing why all the other viable functions lost out. Sometimes this is useful, when you can’t figure out why the function you wanted to be called hasn’t been considered.

    But I agree that mostly it’s just a lot of noise, especially for functions like operator << or operator >> (or even operator []) which have a lot of overloads.

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

Sidebar

Related Questions

OK! I just started HTML and CSS today and thought it would be a
I thought I would share the workaround I discovered today. It is not possible
string filename = DateTime.Today.ToString() + .csv; if(!File.Exists(filename)) File.Create(filename); I thought this would work but
I was having a nice look at my STL options today. Then I thought
I recently started programming JavaScript and thought everything would be good... Well today I
I had a wild thought today. I've been discovering that my users like my
I ran into this question today and thought I should post it for the
As I was writing a for loop earlier today, I thought that there must
I am trying to do something I thought would be relatively simple and get
Thought I would ask the knowledgeable StackOverflow community a question that was on my

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.