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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:19:49+00:00 2026-06-17T08:19:49+00:00

In the code below, I would expect to get a compiler error if more

  • 0

In the code below, I would expect to get a compiler error if more than one cast operator is defined because of the ambiguity.

#include <iostream>
#include <sstream>

struct A
{
    operator const char*() { return "hello world\n"; }
    operator float()       { return 123.0F; }
    //operator int()         { return 49; }
};

int main()
{
    A a;
    std::stringstream ss;
    ss << a;
    std::cout << ss.str();
    return 0;
}

Instead, as long as only one numeric cast operator is defined then it compiles with no errors, no warnings, and the numeric cast is used in preference to the operator const char *(). The order of the declared operators makes no difference.

However if operator int() and operator float() are both defined then I get what I expected from the start:

‘operator <<‘ is ambiguous

Are there precedence rules for casts, or why does the compiler choose the numeric cast by default? I do understand that I should explicitly state which cast I mean, but my question is on the default choice the compiler makes.


Edit: Using compiler MSVC 2010

  • 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-17T08:19:50+00:00Added an answer on June 17, 2026 at 8:19 am

    Conversions are ranked according to § 13.3.3.1 of the C++ Standard. In particular, user-defined conversion sequences pertinent to your example are regulated by § 13.3.3.1.2/1:

    “A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion (12.3) followed by a second standard conversion sequence. […] If the user-defined conversion is specified by a conversion function (12.3.2), the initial standard conversion sequence converts the source type to the implicit object parameter of the conversion function.”

    All conversions sequences here involve:

    1. a fictitious conversion to the source type of the implicit object parameter of the conversion function;
    2. a user-defined conversion;
    3. an identity conversion to the input type of operator <<.

    These conversion sequences all have the same rank. Thus, the call should be ambiguous. If it is not, for me it is a compiler bug.

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

Sidebar

Related Questions

Using the PHP code below, I would expect to get '2' as my output.
In the code below I would expect t2: [{value:.....}, {}, {}] but instead I
I understand that the code below would result segmentation fault because at the cstr
The code below doesn't behave as I would expect. ; given a function name,
Using Rhino Mocks 3.6, given the code below I would expect the AssertWasCalled assertion
The code below works as I would expect it to in that I can
I would expect that my code below would, in the event I selected 8/6/2012
In the code block below I would expect dictCars to contain: { Chevy:Camaro, Dodge:Charger
I am trying to implement does_key_exist, I thought the code below would do it.
if f = g + h then where in the below code would I

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.