Я не уверен, правильно ли я вас понимаю, но эта метапрограмма может сделать то, что вам нужно:
// Just a placeholder type for default template arguments
struct NoType { };
// Template to check whether a type is NoType. You can replace this with boost
// is_same<T, NoType> if you like.
template <typename T>
struct IsNoType {
static const bool value = false;
};
template <>
struct IsNoType<NoType> {
static const bool value = true;
};
// Template for matching the expressions and their corresponding types
// This could be done more nicely using variadic templates but no MSVC
// version supports them currently.
// You can specify up to 8 conditions and types. If you specify more,
// the code will break :) You can add more easily by just expanding the
// number of lines and parameters though.
template <
bool p0 = false, typename t0 = NoType,
bool p1 = false, typename t1 = NoType,
bool p2 = false, typename t2 = NoType,
bool p3 = false, typename t3 = NoType,
bool p4 = false, typename t4 = NoType,
bool p5 = false, typename t5 = NoType,
bool p6 = false, typename t6 = NoType,
bool p7 = false, typename t7 = NoType,
// This must not be changed/overriden/specified, it is used as a condition to stop the compiler loop, see below
bool stop = true, typename stopT = NoType
>
struct GetFirstMatchingType {
};
// Specialization when the first element in the expression list is true.
// When this happens, we just return the first type as the ::type typedef.
template <
typename t0,
bool p1, typename t1,
bool p2, typename t2,
bool p3, typename t3,
bool p4, typename t4,
bool p5, typename t5,
bool p6, typename t6,
bool p7, typename t7,
bool p8, typename t8
>
struct GetFirstMatchingType<true, t0, p1, t1, p2, t2, p3, t3, p4, t4, p5, t5, p6, t6, p7, t7, p8, t8> {
typedef t0 type;
// Check that the type is not NoType. If it is, it means all arguments are false and we should fail.
// In case of a non-C++11 compiler, you can throw any other compiler error here or use BOOST_STATIC_ASSERT
static_assert(!IsNoType<t0>::value, "No expression has been matched, don't know what type to return!");
};
// Specialization when the first type is false. If this happens, we cyclically rotate
// the sequence so that p0, t0 becomes p8, t8. The compiler keeps expanding this
// until it finds true as the first element. Note that this will always happen because
// the stop argument in the base template is set to true.
template <
typename t0,
bool p1, typename t1,
bool p2, typename t2,
bool p3, typename t3,
bool p4, typename t4,
bool p5, typename t5,
bool p6, typename t6,
bool p7, typename t7,
bool p8, typename t8
>
struct GetFirstMatchingType<false, t0, p1, t1, p2, t2, p3, t3, p4, t4, p5, t5, p6, t6, p7, t7, p8, t8> {
typedef typename GetFirstMatchingType<p1, t1, p2, t2, p3, t3, p4, t4, p5, t5, p6, t6, p7, t7, p8, t8, false, t0>::type type;
};
int main() {
// Evaluates to int myVar1 if int is 4 bytes, or __int32 myVar1 if __int32 is 4 bytes and int is not 4 bytes
GetFirstMatchingType<
sizeof(int) == 4, int,
sizeof(__int32) == 4, __int32
>::type myVar1;
// Evaluates to short myVar on my platform
GetFirstMatchingType<
sizeof(int) == 5, int,
sizeof(short) == 2, short
>::type myVar2;
// Also evaluates to short myVar on my platform
GetFirstMatchingType<
sizeof(int) == 5, int,
sizeof(short) == 2, short,
sizeof(int) == 4, int
>::type myVar3;
// Throws an error (error C2338: No expression has been matched, don't know what type to return!)
GetFirstMatchingType<
sizeof(int) == 5, int,
sizeof(long) == 5, long,
sizeof(short) == 3, short
>::type myVar4;
}
Протестировано на MSVC 2010, но оно должно хорошо работать на любом C ++-совместимом компиляторе, таком как GCC или Clang.
РЕДАКТИРОВАТЬВот пример решения вашего вопроса с использованием приведенного выше кода:
struct ScriptVar_t;
struct CastScriptVar1 {
template<typename T> static T castConst(const ScriptVar_t& s) { return (T) s; }
};
struct CastScriptVar2 {
template<typename T> static T castConst(const ScriptVar_t& s) { return *s.as<T>(); }
};
struct ScriptVar_t {
operator int() const { return 0; }
operator float() const { return 0.0f; }
operator BaseObject() const { return BaseObject(); }
template<typename T> T* as() const { return NULL; }
template <typename T> T castConst() const {
return GetFirstMatchingType<
!boost::is_base_of<CustomVar, T>::value, CastScriptVar1,
GetType<T>::value >= SVT_BASEOBJ, CastScriptVar2
// Add more conditions & casts here
>::type::castConst<T>(*this);
}
};
int main()
{
ScriptVar_t v;
v.castConst<int>();
v.castConst<CustomVar>();
return 0;
}
Его можно переписать, используя boost :: tuple и boost :: mpl, чтобы избавиться от странных вариационных шаблонов.
РЕДАКТИРОВАТЬ 2: кажется, мой предыдущий EDIT исчез, я положил его обратно