【发布时间】:2021-03-20 10:48:35
【问题描述】:
我正在尝试使用 C++20 概念实现递归版本 std::iter_value_t,以便可以检索像 std::vector<std::vector<...std::vector<T>...>> 这样的嵌套容器的基本类型 T。实验实现如下。
template<typename T>
concept is_iterable = requires(T x)
{
*std::begin(x);
std::end(x);
};
template<typename T> requires (!is_iterable<T>)
struct recursive_iter_value_t_detail
{
typedef typename T type;
};
template<typename T> requires (is_iterable<T>)
struct recursive_iter_value_t_detail
{
typedef typename std::iter_value_t<typename recursive_iter_value_t_detail<T>::type> type;
};
template<typename T>
using recursive_iter_value_t = typename recursive_iter_value_t_detail<T>::type;
尝试编译此代码后,弹出了唯一的错误消息'recursive_iter_value_t_detail': requires clause is incompatible with the declaration,我不确定requires clause is incompatible with the declaration 是什么意思。是模板结构不能这样重载的问题吗?请帮我解决这个问题。
recursive_iter_value_t<std::vector<std::vector<int>>> 的预期输出是int。
【问题讨论】:
标签: c++ templates recursion c++20 c++-concepts