Wie kann ich den Rückgabetyp einer impliziten Konvertierung eines Objekts erhalten?Get Typ der impliziten Konvertierung
struct Bar {
operator int() const {
return 0;
}
};
// std::result_of<Bar>::type value; ???
// std::result_of<Bar::operator ??? >::type value;
ich verwenden könnte:
std::is_convertible<Bar, int>::value
aber is_convertible gilt auch für Schwimmer, unsigned int etc .... würde Ich mag die genaue Art haben.
Edit: Da meine Frage unklar scheint, warum möchte ich den impliziten Conversion-Typ wissen. Bitte denken Sie einen Schritt weiter zu Template-Klassen. Also ich weiß nicht, Bar überhaupt ...
template<typename T, typename Sfinae = void>
struct ImplicitType
{
static_assert(sizeof(T) != sizeof(T), "Unknown type.");
};
template<typename T>
struct ImplicitType<T,
typename std::enable_if<std::is_convertible<T, int>::value && std::is_class<T>::value>::type>
{
using type = int;
};
template<typename T>
struct ImplicitType<T,
typename std::enable_if<std::is_convertible<T, float>::value && std::is_class<T>::value>::type>
{
using type = int;
};
struct Foo
operator float() const {
return 0.0f;
}
};
struct Bar {
operator int() const {
return 0;
}
};
ImplicitType<Foo> r; // <--- ambiguous template instantiation
ImplicitType<Bar> r; // <--- ambiguous template instantiation
Für Foo möchte ich Float bekommen. Für Bar int.
Aber weil ich eine oder mehrere implizite Konvertierungen für Klasse definieren kann, wird es schwierig.
struct FooBar {
operator float() const {
return 0;
}
operator int() const {
return 0;
}
};
Also alles in allem ist es nicht möglich ist, die richtige implizite Gespräch Typ einer Klasse zu bekommen?
decltype() und Auto? –
*** decltype *** ist Bar und *** auto f = Bar; *** ist Bar. –
Viatorus
Riegel b; decltype ((int) b) x = (int) b; –