Próbowałem znaleźć sposób na ujednoznacznienie tego kodu (w czasie kompilacji) (od dwóch dni :-) -> get_value jest ambugiusem.C++: disambiguate ten kod w czasie kompilacji?
#include <iostream>
template <typename T>
struct type2type {};
template<class T, int val>
struct BASE
{
static constexpr int get_value (type2type<T>)
{
return val;
}
};
class X {};
class Y {};
struct A :
public BASE< X, 1 >,
public BASE< Y, 0 >
{};
int main (int argc, char **argv)
{
A a {};
std::cout << a.get_value (type2type<X>{}) << std::endl;
}
To działające środowisko wykonawcze.
#include <iostream>
template <typename T>
struct type2type {};
template<class T>
struct VIRTUAL
{
int get_value() const
{
return get_value_from_BASE (type2type<T> {});
}
private:
virtual int get_value_from_BASE (type2type<T>) const = 0;
};
template<class T, int val>
class BASE :
public VIRTUAL<T>
{
virtual int get_value_from_BASE (type2type<T>) const override
{
return val;
}
};
class X {};
class Y {};
struct A :
public BASE< X, 1 >,
public BASE< Y, 0 >
{};
int main (int argc, char **argv)
{
A a {};
std::cout << a.::VIRTUAL<X>::get_value() << std::endl;
}
Czy istnieje rozwiązanie?
Uwaga: możliwy sposób, że znalazłem się na std :: is_base_of <>, ale to jest bardzo ograniczona (szablon głębokość instancji)
Państwo zadzwonić get_value na obiekcie typu A, w którym znajdują się dwa obiekty BASE. Który kompilator powinien wybrać? –