I need a template expression that selects the first argument type if the first argument is defined, else the second argument type.
select<int, float>::type // type evaluates to int
select<an_undefined_type, float>::type // type evaluates to float
… and the solution has to work with C++03 and Boost 1.33.1 🙁
My goal is to accept both int and boost::optional<int> as a function template parameter T, so I can do something like:
template<typename T>
void fn(T& t)
{
int x = std::numeric_limits<select<T::value_type, T>::type>::digits;
}
since boost::optional<int>::value_type is defined.
C++11 solutions are also appreciated.
I don’t see a way to do this with template specialization, since I’m trying to specialize not on types but on concepts. Basically, I would need one specialization that matches the any_integer concept and one specialization that matches the boost::optional<any_integer> concept.
I guess with C++11 I could accomplish this specific goal with:
std::conditional<std::is_integral<T>::value, T, T::value_type>::value
but I don’t have C++11, and I want the more general solution.
I don’t think you can achieve the exact notation you are looking for. However, I think you can use a slightly different notation to achieve what you are semantically after. The problem with your current notation
is that
select<T0, T1>expects two types, i.e., the requirement for the types to be present isn’t on theselect<T0, T1>template but on the function calling it. The way I would change this is to useNow all what needs to happen is to have a
get_value_type<T>which yields the nested type if present and some typeselect<T0, T1>is going to ignore if arrives there, e.g.,void(or a custom marker type). Theget_value_type<T>template should be fairly simple (I saw Dirk Holsopple’s answer but I couldn’t get it work):Obviously, you might want to define your type-traits slightly different so you can use something like
This would return the nested type if there is a
value_typeand a typeTotherwise.