C++概念循环
C++ concepts loop
我想让编译器检查元组是否只包含"元类型"。
顺便说一下,我对C++概念是全新的。
template < typename T >
struct Type {
using type = T;
};
//! A type can be easily check with a small concept
template < typename T >
concept bool C_Type = requires {
typename T::type;
};
//! But how to apply it on a whole tuple?
template < typename T >
void foo(T tuple) {}
int main() {
constexpr auto test = std::make_tuple(Type<int>{}, Type<double>{});
foo(test);
}
所以我想确保序列中的每个类型(在这个例子中只说一些可迭代的东西)都是"元类型"。
我正在使用Boost Hana,如果它可以简化代码。
目前我什至不确定这是否可能。我希望是这样,我想我只需要学习更多的元编程知识。所以我会继续搜索和尝试,但如果有人已经有了答案,谢谢!
概念在设计上太弱,无法执行元编程,因此要做到这一点,您需要语言其余部分的一些"元编程帮助"。我将使用模板专用化将类型分解为模板及其类型参数,然后要求所有这些参数满足C_Type
:
template <class>
constexpr bool TypeTuple_ = false;
template <template <class...> class Tuple, class... Types>
requires (C_Type<Types> && ...)
constexpr bool TypeTuple_<Tuple<Types...>> = true;
template <class T>
concept bool TypeTuple = TypeTuple_<T>;
这适用于hana::tuple
和std::tuple
- 任何采用所有类型参数的模板。
我对概念不太熟悉,但你当然可以通过多种方式使用 Boost.Hana 实现这一点。
通过查看注释,应该注意的是,任何元组类型都可以制作成一个hana::Sequence
按照惯例也是hana::Searchable
和hana::Foldable
。
下面是一个将std::tuple
用作hana::Searchable
的示例:
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>
#include <tuple>
namespace hana = boost::hana;
template < typename T >
concept bool C_Type = requires {
typename T::type;
};
auto is_C_Type = hana::overload_linearly([](C_Type const&) { return hana::true_c; },
[](auto const&) { return hana::false_c; });
template < typename T >
constexpr bool foo(T const& tuple) {
return hana::all_of(tuple, is_C_Type);
}
int main() {
constexpr auto test = std::tuple{hana::type_c<int>, hana::type_c<double>};
static_assert(foo(test));
}
https://wandbox.org/permlink/YNZDX7uN6mgUdmje
下面是一个示例,说明如何检查tuple
是否仅包含定义类型名type
的类型。这里的诀窍是定义一个元组类型,该类型为元组std::tuple<T0, ..., TN, TM>
定义一个新的类型std::pair<std::pair<...std::pair<void, T0>, ...TN>, TM>
。这适用于 GCC 7.2。我对如何更干净地组合可变参数约束感兴趣,因为我没有找到任何参考。
#include <array>
#include <tuple>
template<typename T>
struct Type {
using type = T;
};
template<typename Tuple, size_t I = std::tuple_size<Tuple>::value>
struct TupleType {
using type = std::pair<typename TupleType<Tuple, I - 1>::type,
typename std::tuple_element<I - 1, Tuple>::type>;
};
template<typename Tuple>
struct TupleType<Tuple, 0> {
using type = void;
};
template<typename T>
concept bool C_TupleType = requires {
typename TupleType<T>::type;
};
void foo(C_TupleType tuple) { }
int main() {
constexpr auto test = std::make_tuple(Type<int>{}, Type<double>{});
foo(test);
// also works on pair and array
constexpr auto test1 = std::make_pair(Type<int>{}, Type<double>{});
foo(test1);
constexpr std::array<Type<int>, 3> test2;
foo(test2);
// and of course TupleType is also a meta type
constexpr std::array<TupleType<std::pair<int, int>>, 13> test3;
foo(test3);
return 0;
}
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 在循环C++中指定字符串之后,不会打印该字符串