【发布时间】:2021-05-22 07:38:33
【问题描述】:
假设我有 3 个概念:
ostreamableistreamableiostreamable
定义在哪里:
template <typename T>
concept ostreamable = requires (std::ostream& os, T arg) {
{os << arg} -> std::convertible_to<std::ostream&>;
};
template <typename T>
concept istreamable = requires (std::istream& is, T& arg) {
{is >> arg} -> std::convertible_to<std::istream&>;
};
template <typename T>
concept iostreamable = ostreamable<T> && istreamable<T>;
应用:
iostreamable auto var1 = {4, 5, 10, 10}; // no error, but unexpected.
iostreamable auto var2 = 3.232; // no error, expected
iostreamable auto var3 = std::bitset<4>{0b1001}; // no error, expected
iostreamable auto var4 = std::vector{4, 10, 3, 10}; // compilation error, expected
当我将var1 与上述概念iostreamable 的要求一起使用时,例如使用operator<< 或operator>>,我预计会得到详细的编译错误。
【问题讨论】:
标签: c++ initializer-list c++-concepts