我正在玩模板,并试图实现以下帮助.
first_constructible<Types..., Args...>::type
Run Code Online (Sandbox Code Playgroud)
它将返回第一种Types可构造的类型Args....第一个问题显然是有两个参数包struct,所以我改用了
first_constructible<std::tuple<Types...>, Args...>::type
Run Code Online (Sandbox Code Playgroud)
我已经通过拆分元组类型作为第一个和休息来实现它,检查使用std::is_constructible并在必要时递归.
template<typename T>
struct pop_front_tuple
{
template<typename U, typename... Us>
static std::tuple<Us...> impl(std::tuple<U, Us...>);
using type = decltype(impl(std::declval<T>())); // std::tuple with removed first type
};
template<typename Tuple, typename... Args>
struct first_constructible
{
using first_type = decltype(std::get<0>(std::declval<Tuple>()));
using type = typename std::conditional
<
std::is_constructible<first_type, Args...>::value,
first_type,
typename first_constructible<typename pop_front_tuple<Tuple>::type, Args...>::type
>::type;
};
// end of recursion
template<typename... Args>
struct first_constructible<std::tuple<>, Args...>
{
using type = void;
}; …Run Code Online (Sandbox Code Playgroud)