2016-03-30 11 views
0

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; 
    } 
}; 

Not working live example.

Also alles in allem ist es nicht möglich ist, die richtige implizite Gespräch Typ einer Klasse zu bekommen?

+0

decltype() und Auto? –

+0

*** decltype *** ist Bar und *** auto f = Bar; *** ist Bar. – Viatorus

+0

Riegel b; decltype ((int) b) x = (int) b; –

Antwort

-1
#include <iostream> 
#include <typeinfo> 
struct Bar { 
    operator int() const { 
     return 0; 
    } 
    operator double() const { 
     return 0.0; 
    } 

    struct Foo { 

    }; 
    operator Foo() const { 
     return Foo(); 
    } 
}; 
int main() { 
    std::cout << typeid(decltype((int)Bar())).name(); 
    std::cout << std::endl; 
    std::cout << typeid(decltype((double)Bar())).name(); 
    std::cout << std::endl; 
    std::cout << typeid(decltype((Bar::Foo)Bar())).name(); 
    std::cout << std::endl; 
} 

Nach dieser Tatsache, dass Funktion Bar::operator int() ist eine Elementfunktion der Klasse Bar, Sie garantieren, dass es eine this Referenz für sie, also, warum ich ein Objekt Standard bieten Bar() für die ganze Zeug.

Das Ergebnis ist:

i 
d 
N3Bar3FooE 
+0

'declltype ((int) X)' gibt immer 'int &', nichts mit 'Bar' zu tun ... obwohl die OP-Frage unklar ist –

+0

OPs Frage ist unklar, aber offensichtlich ist es nicht:" Was ist der Rückgabetyp von Alles :: operator int() ". Es wäre schön, wenn jemand überhaupt über implizite Conversions antworten würde. – Pleeea

+0

Ich bearbeite meine Frage. Bitte schau, ob du mich jetzt verstehst. Bar weiß ich nicht, also ist ein (C) -Cast nicht möglich. – Viatorus