【发布时间】:2021-10-05 20:25:59
【问题描述】:
考虑 Boost.MP11、Brigand 等库提供的任何常见类型级算法... 例如:
template<typename... Args>
struct TypeList;
using my_types = TypeList<int, float, char, float, double>;
constexpr int count = boost::mp11::mp_count_if<my_types, std::is_floating_point>::value;
// this holds:
static_assert(count == 3);
注意std::is_floating_point 可以定义为:
template<typename T>
struct is_floating_point { constexpr bool value = __compiler_magic(T); };
同样,我们有std::floating_point 概念
template<typename T>
concept floating_point = requires (T t) { __other_compiler_magic(T); };
遗憾的是,尽管有相似之处,但如果不为概念引入手动命名的包装器,似乎没有一种简单的方法可以编写这样的东西:
constexpr int count = boost::mp11::count_if<my_types, std::floating_point>::value;
我的问题是:为什么此时不能传递概念来代替类型?是缺乏标准化,还是这些库可以通过提供更多重载来解决?
看起来每个概念都必须包装在一个模板类型中,该类型只会在其模板参数上调用该概念。
从外部看,概念就像元函数,其域是 {set of types} -> bool。编译器能够延迟将参数传递给“传统”基于类型的元函数,例如std::is_floating_point,为什么概念上似乎不能发生同样的情况?
【问题讨论】:
标签: c++ c++20 template-meta-programming c++-concepts