Search

decltype appears to drop const qualifiers for overloaded operators by Mooing Duck

Closed
as Fixed Help for as Fixed

2
0
Sign in
to vote
Type: Bug
ID: 743026
Opened: 5/21/2012 5:46:01 PM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
1
User(s) can reproduce this bug
In a trailing decltype for a function's return type (possibly other cases), the decltype ignores the const qualifiers of the parameters, causing it to use the wrong operator overloads. In this case the compiler compiled when it should have emitted a compiler error.

Compiler correctly does not compile when the operator is replaced with a member function.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 11 Beta

Steps to reproduce

struct X{};

bool operator>(X& a, int const& b){ return true; }

template<class T, class U>
auto operator<(T const& v, U const& u) -> decltype(v > u){ return true; }

int main(void) {
X x;
x < 6;
}

Product Language

English

Operating System

Windows XP

Operating System Language

English

Actual results

Compiles and executes an infinite loop

Expected results

Compiler error.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 5/24/2012 at 1:05 PM
Thank you for contacting Microsoft. We now correctly give an error on your example case as the type of "v" is "X const &" while the argument of operator> is X& and type deduction for operator< fails when T=X and U=int is substituted in.

Tanveer Gani
Visual C++ Team.
Posted by MS-Moderator07 [Feedback Moderator] on 5/21/2012 at 9:56 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by MS-Moderator01 on 5/21/2012 at 6:43 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Posted by c9xeo on 5/21/2012 at 6:08 PM
Here's a snippet that correctly fails to compile in VS11 beta:

struct X{};

bool f(X& a, int const& b){ return true; }

template<class T, class U>
auto g(T const& v, U const& u) -> decltype(f(v,u)){ return true; }

int main(void) {
X x;
g(x, 6);
}

One can even change the original code to use the free function calling form in the trailing return type and, again, it correctly fails to compile:

struct X{};

bool operator>(X& a, int const& b){ return true; }

template<class T, class U>
auto operator<(T const& v, U const& u) -> decltype(operator>(v,u)){ return true; }

int main(void) {
X x;
x < 6;
}

Somehow, overload resolution inside a trailing return type's decltype discards cv qualifiers on all arguments. Here's another snippet that shows the strange behaviour:

struct X{};

bool operator>(X const& a, int& b){ return true; }

template<class T, class U>
auto operator<(T volatile& v, U volatile& u) -> decltype(v > u){ return true; }

int main(void) {
X x; int i = 6;
x < i;
}
Sign in to post a workaround.