mercredi 4 avril 2018

User-defined conversion operator template and built-in operators: no match for operator

Consider the following MCVE.

#include <type_traits>

struct A {
    template<typename T, typename std::enable_if<std::is_same<T,int>::value,int>::type = 0>
    operator T() const { return static_cast<T>(1); }
};

int main() {
    int x = 1;
    A a;
    return x + a;
}

clang compiles it fine. DEMO

But GCC fails with:

error: no match for 'operator+' (operand types are 'int' and 'A')
  return x + a;
         ~~^~~

Question: who is right and why?

Aucun commentaire:

Enregistrer un commentaire