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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:37:54+00:00 2026-05-28T06:37:54+00:00

This example seems to compile with VC10 and gcc (though my version of gcc

  • 0

This example seems to compile with VC10 and gcc (though my version of gcc is very old).

EDIT: R. Martinho Fernandez tried this on gcc 4.7 and the behaviour is still the same.

struct Base
{
    operator double() const { return 0.0; }
};

struct foo
{
    foo(const char* c) {}
};

struct Something : public Base
{
    void operator[](const foo& f) {}
};

int main()
{
    Something d;
    d["32"];

    return 0;
}

But clang complains:

test4.cpp:19:6: error: use of overloaded operator '[]' is ambiguous (with operand types 'Something' and 'const char [3]')
    d["32"]
    ~^~~~~
test4.cpp:13:10: note: candidate function
    void operator[](const foo& f) {}
         ^
test4.cpp:19:6: note: built-in candidate operator[](long, const char *)
    d["32"]
     ^
test4.cpp:19:6: note: built-in candidate operator[](long, const restrict char *)
test4.cpp:19:6: note: built-in candidate operator[](long, const volatile char *)
test4.cpp:19:6: note: built-in candidate operator[](long, const volatile restrict char *)

The overload resolution is considering two possible functions from looking at this expression:

  • calling Something::operator[] (after a user defined conversion)
  • calling built in operator for const char* (think “32”[d]) (after a user defined conversion and standard conversion double to long).

If I had written d["32"] as d.operator[]("32"), then overload resolution won’t even look at option 2, and clang will also compile fine.

EDIT: (clarification of questions)

This seems to be a complicated area in overload resolution, and because of that I’d appreciate very much answers that explain in detail the overload resolution in this case, and cite the standard (if there’s some obscure/advanced likely to be unknown rule).

If clang is correct, I’m also interested in knowing why the two are ambiguous / one is not preferred over another. The answer likely would have to explain how overload resolution considers implicit conversions (both user defined and standard conversions) involved on the two candidates and why one is not better than the other.

Note: if operator double() is changed to operator bool(), all three (clang, vc, gcc) will refuse to compile with similar ambiguous error.

  • 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-28T06:37:54+00:00Added an answer on May 28, 2026 at 6:37 am

    It should be easier to picture why the overload resolution is ambiguous by going through it step-by-step.

    §13.5.5 [over.sub]

    Thus, a subscripting expression x[y] is interpreted as x.operator[](y) for a class object x of type T if T::operator[](T1) exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3.3).

    Now, we first need an overload set. That’s constructed according to §13.3.1 and contains member aswell as non-member functions. See this answer of mine for a more detailed explanation.

    §13.3.1 [over.match.funcs]

    p2 The set of candidate functions can contain both member and non-member functions to be resolved against the same argument list. So that argument and parameter lists are comparable within this heterogeneous set, a member function is considered to have an extra parameter, called the implicit object parameter, which represents the object for which the member function has been called. […]

    p3 Similarly, when appropriate, the context can construct an argument list that contains an implied object argument to denote the object to be operated on.

    // abstract overload set (return types omitted since irrelevant)
    f1(Something&, foo const&); // linked to Something::operator[](foo const&)
    f2(std::ptrdiff_t, char const*); // linked to operator[](std::ptrdiff_t, char const*)
    f3(char const*, std::ptrdiff_t); // linked to operator[](char const*, std::ptrdiff_t)
    

    Then, an argument list is constructed:

    // abstract argument list
    (Something&, char const[3]) // 'Something&' is the implied object argument
    

    And then the argument list is tested against every member of the overload set:

    f1 -> identity match on argument 1, conversion required for argument 2
    f2 -> conversion required for argument 1, conversion required for argument 2 (decay)
    f3 -> argument 1 incompatible, argument 2 incompatible, discarded
    

    Then, since we found out that there are implicit conversions required, we take a look at §13.3.3 [over.match.best] p1:

    Define ICSi(F) as follows:

    • if F is a static member function, […]; otherwise,
    • let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F. 13.3.3.1 defines the implicit conversion sequences and 13.3.3.2 defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another.

    Now let’s construct those implicit conversion sequences for f1 and f2 in the overload set (§13.3.3.1):

    ICS1(f1): 'Something&' -> 'Someting&', standard conversion sequence
    ICS2(f1): 'char const[3]' -> 'foo const&', user-defined conversion sequence
    ICS1(f2): 'Something&' -> 'std::ptrdiff_t', user-defined conversion sequence
    ICS2(f2): 'char const[3]' -> 'char const*', standard conversion sequence
    

    §13.3.3.2 [over.ics.rank] p2

    a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence.

    So ICS1(f1) is better than ICS1(f2) and ICS2(f1) is worse than ICS2(f2).
    Conversely, ICS1(f2) is worse than ICS1(f1) and ICS2(f2) is better than ICS2(f1).

    §13.3.3 [over.match.best]

    p1 (cont.) Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then […]

    p2 If there is exactly one viable function that is a better function than all other viable functions, then it is the one selected by overload resolution; otherwise the call is ill-formed.

    Well, f*ck. 🙂 As such, Clang is correct in rejecting that code.

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

Sidebar

Related Questions

This seems like a very simple and a very common problem. The simplest example
in this example, it seems that we can use a variable (here second) to
I was reading thourg this example (link) ... From the description, this seems paradox
I have this simple example I can't seems to get working : MERGE INTO
For example this article introduces them. What is the benefit? Static analysis seems cool
This is an example from a book, a bit modified. It seems like sessions
It seems Scrum and Agile tests/assertions are becoming popular this year. For example, Nokia
I am learning ADO.NET entity framework. This is my first example. The datagrid seems
I was reading CLR via C# and it seems that in this example, the
I've got this to work using a very basic example(outlined below) and I want

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.