Bug 62110 - Attempting to use template conversion operator in a contextual conversion (switch)
Summary: Attempting to use template conversion operator in a contextual conversion (sw...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2014-08-12 19:26 UTC by Shafik Yaghmour
Modified: 2023-12-17 18:52 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.8.2, 4.9.1, 5.0
Last reconfirmed: 2021-08-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Shafik Yaghmour 2014-08-12 19:26:54 UTC
Given the following code:

class Var
{
public:

    operator int () const
    { return 0; }

    template <typename T>
    operator T () const
    { return T(); }

};

int main()
{
    Var v;
    switch (v)
    { }
}

gcc 4.9 produces the following error:

main.cpp: In function 'int main()':

main.cpp:17:14: error: default type conversion can't deduce template argument for 'template<class T> Var::operator T() const'
     switch (v)
              ^

using the following command line options:

  -std=c++11 -Wall -Wextra -Wconversion -pedantic
 
Using -std=c++1y also produces the same error.

while clang 3.4 does not produce any errors. As far as I can tell clang is correct here for C++1y and is probably correct for C++11 although that may depend on whether you consider  N3323: "A Proposal to Tweak Certain C++ Contextual Conversions" to be a fix for C++11 or part of C++1y. 

N3323 is incorporated in N3485 which I consider to be C++11 with fixes but perhaps that is an incorrect interpretation. Based on this assumption then if we look at section 6.4.2 it says:

The condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted (Clause 4) to an integral or enumeration type.

and this would force us to use section 4 paragraph 5 which does not allow or overload resolution making int conversion the only one available for this context.

If N3323 is not part of C++11 then it seems unclear to me whether the template conversion function should be considered or not.

This bug report comes the following Stackoverflow question:

http://stackoverflow.com/questions/25047109/classes-with-both-template-and-non-template-conversion-operators-in-the-conditio
Comment 1 Jonathan Wakely 2014-08-12 20:31:28 UTC
N3323 is not addressing a DR, so is not part of C++11.
Comment 2 TC 2014-08-12 23:27:09 UTC
According to https://gcc.gnu.org/projects/cxx1y.html, N3323 should have been implemented in GCC 4.9 with -std=c++1y.