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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:03:11+00:00 2026-06-05T06:03:11+00:00

First, a tiny example that demonstrates the problem: struct Bar { enum Baz {aa,

  • 0

First, a tiny example that demonstrates the problem:

struct Bar {
    enum Baz {aa, bb, cc};
    Baz baz_;

    operator Baz() const { return baz_; }
    private:
        template<typename T> operator T() const;
};

int main() {
    Bar bar;
    switch (bar) {
        case Bar::aa:
            break;
        case Bar::bb:
            break;
        case Bar::cc:
            break;
        default:
            break;
    }
    return 0;
}

Compiling this code with g++ 4.7.0 gives the following error:

foo.cpp: In function ‘int main()’:
foo.cpp:12:16: error: ambiguous default type conversion from ‘Bar’
foo.cpp:12:16: error:   candidate conversions include ‘template<class T> Bar::operator T() const’

My understanding is that, since the struct object is being “switched” on, the compiler will try to find a conversion function to an integral or enum type. I explicitly provide a public conversion function to the Bar::Baz enum type and would like it to use that.

The confusing part to me is that the compiler also finds the private conversion function and then cannot decide which to use. Why is it even considering the private function? If I add an explicit cast, say switch((int)bar), then only the private conversion function matches and the compiler rightly complains that it cannot use it since it’s private. So, since the private conversion function can’t be used in this context, why isn’t the choice between the two unambiguous?

Interestingly, I believe (though I’m not 100% sure) this code compiles without error on g++ 4.6.

edit: As pointed out by James McNellis in the comments, the fact that the private conversion function is templated is also relevant here.

  • 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-05T06:03:13+00:00Added an answer on June 5, 2026 at 6:03 am

    Access control comes after overload resolution. This is specified in the standard, §13.3

    Overload resolution is a mechanism for selecting the best function to call given a list of expressions that are
    to be the arguments of the call and a set of candidate functions that can be called based on the context of the
    call. The selection criteria for the best function are the number of arguments, how well the arguments
    match the types of the parameters of the candidate function, how well (for nonstatic member functions) the
    object matches the implied object parameter, and certain other properties of the candidate function. [Note:
    the function selected by overload resolution is not guaranteed to be appropriate for the context. Other
    restrictions, such as the accessibility of the function, can make its use in the calling context ill-formed. ]

    So the overload resolution can chose a function that is not appropriate for the given context.

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

Sidebar

Related Questions

I got a tiny problem. First time I am doing a UL menu with
first of all, sorry if that question is dumb but I´m a total newbie
First I've read loads of posts and sites that recommend going to http://silverlight.net/GetStarted/ to
I have one page that displays a bar graph, and the values can vary
I need a tree structure that supports and and oring. For example, given a
I've got a tiny webapp with index.jsp that forwards (it mostly only contains): <jsp:forward
I'm following the Create your First Tiny MVC Boilerplate with PHP tutorial, and as
Hi All It's the first time I've used Tiny Mce and I have a
I'm working in tiny project that deal with multiple file uploading. at the begining
A couple of months ago I've coded a tiny tool that we needed at

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.