【发布时间】:2020-07-05 21:57:20
【问题描述】:
在调用std::contional<bool, T1, T2> 时,T1 和 T2 类型都会在调用时立即实例化,无论 bool 的值如何,是否存在仅实例化相关类型的实现(或一些实现它的指针) .
例如以下代码是不可能使用 std::conditional 编写的。我们甚至不能在结构上使用 SFINAE(参见示例)。
struct Term {};
template <int N, typename T, typename ...Ts>
struct PairList
{
static constexpr int i = N;
using type = T;
using tail = PairList<N + 1, Ts...>;
};
template <int N, typename T>
struct PairList<N, T>
{
static constexpr int i = N;
using type = T;
using tail = Term;
};
template <int N, typename pairList>
struct get
{
static constexpr auto good = (N == pairList::i);
using T = typename std::conditional<
good,
typename pairList::type,
typename get<N, typename pairList::tail>::T>::type; // The second parameter might not be defined
};
【问题讨论】:
-
将
conditional<A,B::type,C::type>替换为conditional<A,B,C>::type。
标签: c++ stl c++17 template-meta-programming sfinae