Clang vs GCC vs MSVC Template Konvertierungsoperator - welcher Compiler ist richtig?

Ich habe einfachen Code mit Konvertierungsoperator und es scheint, dass alle Compiler unterschiedliche Ergebnisse liefern. War ich neugierig, welcher Compiler, wenn überhaupt, korrekt ist? Ich habe auch verschiedene Kombinationen ausprobiert, aber die folgenden sind die interessantesten. Code wurde unter Verwendung des C ++ 11-Flags kompiliert, das gleiche Verhalten kann jedoch auch in C ++ 03 beobachtet werden.

#include <iostream>

struct call_operator {
    template<typename T>
    operator T() {
        std::cout << __FUNCTION__ << std::endl;
        return {};
    }

    template<typename T>
    operator const T&() const {
        std::cout << __FUNCTION__ << std::endl;
        static T t;
        return t;
    }

    template<typename T>
    operator T&() const {
        std::cout << __FUNCTION__ << std::endl;
        static T t;
        return t;
    }
};

int main() {
    (void)static_cast<int>(call_operator());
    (void)static_cast<const int&>(call_operator());
    (void)static_cast<int&>(call_operator());
}

clang-3.6:

operator int
operator const int &
operator int &

g ++ - 4.9:

operator T
operator const T&
operator T&

msvc 2014 CTP:

call_operator.cpp(17): error C2440: 'static_cast': cannot convert from 'call_operator' to ' const int &'

nach Entfernung von:

template<typename T>
operator T();

msvc kompiliert:

call_operator::operator const int &
call_operator::operator const int &
call_operator::operator int &

weiterhin nach Entfernen von const in

template<typename T>
operator const T&();

clang-3.6:

call_operator.cpp:26:9: error: ambiguous conversion for static_cast from 'call_operator' to 'int' (void)static_cast<int>(call_operator());

g ++ - 4.9:

operator T
operator const T&
operator T&

msvc 2014 CTP:

call_operator.cpp(16): error C2440: 'static_cast': cannot convert from 'call_operator' to 'int'

Antworten auf die Frage(1)

Ihre Antwort auf die Frage