gpt4 book ai didi

c++ - 您如何为标准库容器定义 C++ 概念?

转载 作者:行者123 更新时间:2023-12-05 08:31:02 24 4
gpt4 key购买 nike

我想要一个标准库容器的 C++ 概念的实现。

非常感谢!

最佳答案

用 C++ 概念库修复了 Caleth 的答案:

template <class ContainerType> 
concept Container = requires(ContainerType a, const ContainerType b)
{
requires std::regular<ContainerType>;
requires std::swappable<ContainerType>;
requires std::destructible<typename ContainerType::value_type>;
requires std::same_as<typename ContainerType::reference, typename ContainerType::value_type &>;
requires std::same_as<typename ContainerType::const_reference, const typename ContainerType::value_type &>;
requires std::forward_iterator<typename ContainerType::iterator>;
requires std::forward_iterator<typename ContainerType::const_iterator>;
requires std::signed_integral<typename ContainerType::difference_type>;
requires std::same_as<typename ContainerType::difference_type, typename std::iterator_traits<typename
ContainerType::iterator>::difference_type>;
requires std::same_as<typename ContainerType::difference_type, typename std::iterator_traits<typename
ContainerType::const_iterator>::difference_type>;
{ a.begin() } -> typename ContainerType::iterator;
{ a.end() } -> typename ContainerType::iterator;
{ b.begin() } -> typename ContainerType::const_iterator;
{ b.end() } -> typename ContainerType::const_iterator;
{ a.cbegin() } -> typename ContainerType::const_iterator;
{ a.cend() } -> typename ContainerType::const_iterator;
{ a.size() } -> typename ContainerType::size_type;
{ a.max_size() } -> typename ContainerType::size_type;
{ a.empty() } -> bool;
};

关于c++ - 您如何为标准库容器定义 C++ 概念?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60449592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com