Kindof similar to Conditional compile-time inclusion/exclusion of code based on template argument(s)?.
(BTW Answer 2 in above question does not compile at all)
(C++11 and above)
I the following class description, would like Outer::Inner<...> to be a type, so as to used as template parameters, etc.
template<typename T>
struct Outer {
__enable_if__(cond_1<T>()) {
// Does not compile if T fails cond_1
template<typename> Inner { ... };
}
__enable_if__(cond_2<T>()) {
// Does not compile if T fails cond_2
template<typename> Inner { ... };
}
};
I could only think of sth. like below.
#include <type_traits>
struct BaseA { static constexpr int va = 111; };
struct BaseB { static constexpr int vb = 222; };
struct C : public BaseA {};
struct D : public BaseB {};
template<typename T>
struct Outer {
template<typename, int selector> struct InnerImpl;
template<typename S> struct InnerImpl<S, 1> { static constexpr int v = T::va; };
template<typename S> struct InnerImpl<S, 2> { static constexpr int v = T::vb; };
static constexpr int computeSelector() {
// Could be less horrible with C++14 constexpr functions
// std::is_base_of acts as a demo of complex compile-time conditions
return std::is_base_of<BaseA, T>::value + std::is_base_of<BaseB, T>::value * 2;
} // **1
template<typename S> using Inner = InnerImpl<S, computeSelector()>;
};
The problem is that this way it is not extendable at all.
Outer is dependent to all possible T, which could easily lead to circular header dependency.
Is there any solution that Outer is not dependent to T?