ho provato a trovare un modo per disambiguare questo codice (in fase di compilazione) (da due giorni :-) -> get_value è ambiguo.C++: disambigua questo codice al momento della compilazione?
#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;
}
Questa è una soluzione runtime funzionante.
#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;
}
C'è una soluzione?
Nota: una possibile via che ho trovato è finita std :: is_base_of <>, ma questo è molto limitata (modello di istanza di profondità)
ti chiamano Get_Value su un oggetto di tipo A, che ha all'interno due oggetti BASE. Quale compilatore dovrebbe scegliere? –