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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:20:56+00:00 2026-06-06T12:20:56+00:00

Can someone explain the rules of casting, and when a conversion is ambiguous? I’m

  • 0

Can someone explain the rules of casting, and when a conversion is ambiguous? I’m getting slightly confused by the following case, which gives different answers on MSVC++ (Visual Studio 2010) and gcc-4.3.4.

#include <string>

class myStr
{
  std::string value;

public:
  myStr(const char* val) : value(val) {}
  operator const char*() const {return value.c_str();}
  operator const std::string() const {return value;}
};

myStr byVal();
myStr& byRef();
const myStr& byConstRef();

int main(int, char**)
{
  myStr foo("hello");
  std::string test;

  // All below conversions fail "ambiguous overload for 'operator='" in gcc
  // Only the indicated coversions fail for MSVC++
  test = foo;  // MSVC++ error "'operator =' is ambiguous"
  test = static_cast<std::string>(foo);

  test = byVal();  // MSVC++ error "'operator =' is ambiguous"
  test = static_cast<std::string>(byVal());  // MSVC++ error 
             // "'static_cast' : cannot convert from 'myStr' to 'std::string'"

  test = byRef();  // MSVC++ error "'operator =' is ambiguous"
  test = static_cast<std::string>(byRef());

  test = byConstRef();  // MSVC++ error "'operator =' is ambiguous"
  test = static_cast<std::string>(byConstRef());

  return 0;
}

What rules govern which of those conversions is legal? And is there any compliant way to use unambiguously a class like myStr which defines casts to both const char* and const std::string?

  • 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-06T12:20:58+00:00Added an answer on June 6, 2026 at 12:20 pm

    The implicit conversions are all ambiguous, since std::string has overloaded assignment operators taking both const std::string& and const char*. This means that both of your conversion operators are equally good choices, hence the ambiguity:

    myStr -> std::string -> operator=(const std::string&)
    myStr -> const char* -> operator=(const char*)
    

    The ambiguities with static_cast are because you’re using the cast to create a temporary std::string object. It’s equally valid to create that from either a std::string or a const char*, so again both of your conversion operators are considered.

    myStr -> std::string -> static_cast<std::string>(std::string)
    myStr -> const char* -> static_cast<std::string>(const char*)
    

    You can break the ambiguity by instead casting to a reference:

    test = static_cast<const std::string&>(foo);
    

    This will still create a temporary, since the conversion operator returns an object. However, that conversion is now implicit, and so can’t involve more than one user-defined conversion; therefore, it can only be done via your operator std::string() and there is no ambiguity.

    myStr -> std::string -> static_cast<const std::string&>(std::string)
    myStr -> const char* -> std::string -> static_cast<const std::string&>(std::string)
          ^^             ^^  two implicit user-defined conversions - not allowed
    

    You might also consider changing the conversion operator to return a const reference, so that the unnecessary temporary can be avoided.

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

Sidebar

Related Questions

Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain to me the rules around what can and cannot be evaluated/inserted
Can someone explain which indexed search would be faster, listing from fastest, to slowest
Can someone explain and give example code to do the following steps in my
I have followed all the rules of memory management but can someone explain to
Can someone explain to me why the following code does not work? public class
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,
Can someone explain why it's best use is for prototyping? Should it not be

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.