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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:05:42+00:00 2026-05-30T05:05:42+00:00

#include <iostream> template< typename U > struct base { template< typename T > base

  • 0
#include <iostream>

template< typename U >
struct base {
    template< typename T >
    base const & operator<<( T x ) const {
        std::cout << sizeof( x ) << std::flush;
        return *this;
    }
};

template< typename U >
struct derived : public base< U > {
    using base<U>::operator<<;

    derived const & operator<<( float const & x ) const {
        std::cout << "derived" << std::flush;
        return *this;
    }
};

int main() {
    unsigned char c( 3 );
    derived< double > d;
    d << c;
    d.operator<<( c );
    return 0;
}

Can you please explain the rules involved to obtain a correct answer of the above code (overloading and overriding in connection with templates, integral promotion, …)? Is it valid? If the rules are too lengthy please provide literature pointers. The latest compilers disagree about the correct result. gcc-4.6 and icpc-12.1.0 claim “11” is the correct answer but VS2010 refuses to compile d << c; due to ambiguities but accepts d.operator<<( c );. The latter outputs 1 iirc. So who’s right and who’s wrong?

  • 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-30T05:05:43+00:00Added an answer on May 30, 2026 at 5:05 am

    “11” is the correct output.

    For both expressions both the derived operator<< and base operator<< are candidates. Then the candidates are compared based on the implicit conversion sequences they require. Because the base operator<< is a template function where the type T has been deduced to match the argument it comes up as the better match in both cases.

    The exact rules are pretty long. You can find the details in section 13.3 Overload resolution of the current C++ draft standard, n3337 linked to in this list of the working group’s papers.

    If you’re asking why MSVC doesn’t compile the one statement, I’m not exactly sure, but a function call is ambiguous when there are multiple computed ICSs that are not better than each other (as defined in 13.3.3). MSVC seems to be computing the wrong ICS for at least one of the overloads in the case of d << c, but the diagnostic doesn’t give any more details:

    error C2666: 'derived<U>::operator <<' : 2 overloads have similar conversions
          with
          [
              U=double
          ]
          ConsoleApplication1.cpp(24): could be 'const derived<U> &derived<U>::operator <<(const float &) const'
          with
          [
              U=double
          ]
          ConsoleApplication1.cpp(14): or       'const base<U> &base<U>::operator <<<unsigned char>(T) const'
          with
          [
              U=double,
              T=unsigned char
          ]
          while trying to match the argument list '(derived<U>, unsigned char)'
          with
          [
              U=double
          ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include stdafx.h #include <iostream> using std::cout; template<class T> class IsPolymorphic { template<class T> struct
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
In the following code: #include <iostream> template <typename T, size_t N> void cal_size(T (&a)[N])
I've written this beauty: #include <iostream> struct something { static const char ref[]; };
#include <iostream> template<typename T, int = 0> struct test { typedef char type[3]; };
#include <iostream> struct A { bool f( int a ) { std::cout << int
I have the following test code #include <iostream> template <typename T> struct PS {
In this code (ready to compile): #include stdafx.h #include <iostream> #include <sstream> using std::cout;
Consider the following use of template template parameters... #include <iostream> template <typename X> class
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class

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.