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

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 743026 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 5/21/2012 5:46:01 PM
Access Restriction Public


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.
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(
Posted by __xeo 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;