标签: c++-concepts

为什么Sortable概念需要完全有序的值类型,而std :: sort只需要"小于"可比性?

关于概念N3701最新论文中,该sort算法有以下示例:

template<typename Cont>
  requires Sortable<Cont>()
void sort(Cont& cont)
Run Code Online (Sandbox Code Playgroud)

其中Sortable概念被定义为

template<typename T>
concept bool Sortable()
{
  return Permutable_container<T>() && Totally_ordered<Value_type<T>>();
}
Run Code Online (Sandbox Code Playgroud)

其中Totally_ordered,毫不奇怪,定义为

template<typename T>
constexpr bool Totally_ordered()
{
  return Weakly_ordered<T>() && Equality_comparable<T>();
}
Run Code Online (Sandbox Code Playgroud)

反过来Equality_comparable被定义为

template<typename T>
constexpr bool Equality_comparable()
{
  return requires(T a, T b) {
    {a == b} -> bool;
    {a != b} -> bool;
  };
}
Run Code Online (Sandbox Code Playgroud)

我没有找到定义Weakly_ordered,但我相信它应该是这样的(我是对的吗?)

template<typename T>
constexpr bool Weakly_ordered()
{
  return requires(T …
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++17

13
推荐指数
1
解决办法
693
查看次数

C++20 概念语法 - requires 参数变量是什么意思?

很多文章都给出了这样的例子:

template<typename T>
concept Equal = requires(T a, T b) {
    { a == b } -> std::same_as<bool>;
};
Run Code Online (Sandbox Code Playgroud)

如果我写:

template<typename T>
concept Equal = requires(T a) {
    { a == a } -> std::same_as<bool>;
};
Run Code Online (Sandbox Code Playgroud)

如果不是,为什么语法是这样设计的?为什么他们要我声明这些变量,如ab

为什么我需要多个相同类型的变量?为什么我什至需要变量?

template<typename T>
concept Equal = requires {
    { declval<const T&>() == declval<const T&>() }
        -> std::same_as<bool>;
};
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++20

13
推荐指数
1
解决办法
701
查看次数

假设的,以前的C++ 0x概念问题

(序言:我是C++ 0x游戏的后续追随者,最近有关从C++ 0x标准中删除概念的争议促使我更多地了解它们.虽然我明白我的所有问题都是完全的假设 - 只要概念在未来一段时间内不是有效的C++代码,如果有的话 - 我仍然有兴趣更多地了解概念,特别是考虑到它将如何帮助我更充分地理解最近的决定背后的优点和随之而来的争议)

在阅读了一些关于概念的介绍性材料之后,就像C++ 0x(直到最近)提出它们一样,我在思考一些语法问题时遇到了麻烦.不用多说,这是我的问题:

1)支持特定派生概念的类型(隐式地,通过auto关键字,还是显式地通过concept_maps)是否也需要独立支持基本概念?换句话说,从另一个(例如concept B<typename T> : A<T>)派生概念的行为是否隐含地包含'隐形'需求声明(在B中requires A<T>;)?混淆源于维基百科关于概念的页面,其中指出:

与类继承一样,满足派生概念要求的类型也满足基本概念的要求.

这似乎说一种类型只需要满足派生概念的要求,而不一定是基本概念的要求,这对我来说没有意义.我知道维基百科远非一个明确的来源; 以上描述只是一个不好的选择?

2)列出类型名称的概念可以是"自动"吗?如果是这样,编译器将如何自动映射这些类型名称?如果没有,是否有任何其他场合在概念上使用'auto'无效?

为澄清,请考虑以下假设代码:

template<typename Type>
class Dummy {};

class Dummy2 { public: typedef int Type; };

auto concept SomeType<typename T>
{
     typename Type;
}

template<typename T> requires SomeType<T>
void function(T t)
{}

int main()
{
    function(Dummy<int>()); //would this match SomeType?
    function(Dummy2()); //how about this?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这些类中的任何一个都会匹配SomeType吗?或者是涉及类型名称的概念所必需的concept_map?

3)最后,我很难理解允许定义哪些公理.例如,我是否可以定义一个逻辑上不一致的公理,例如

concept SomeConcept<typename T>
{
    T operator*(T&, int);

    axiom …
Run Code Online (Sandbox Code Playgroud)

c++ axiom c++11 c++-concepts

12
推荐指数
1
解决办法
463
查看次数

如何在编译时检查表达式是非法的?

我的应用程序中有一个问题,我想声明函数应用程序将被编译器拒绝.有没有办法用SFINAE来检查?

例如,假设我想验证std::transformconst范围是违法的.这是我到目前为止所拥有的:

#include <algorithm>
#include <functional>
#include <iostream>

namespace ns
{

using std::transform;

template<typename Iterator1, typename Iterator2, typename UnaryFunction>
  struct valid_transform
{
  static Iterator1 first1, last1;
  static Iterator2 first2;
  static UnaryFunction f;

  typedef Iterator2                   yes_type;
  typedef struct {yes_type array[2];} no_type;

  static no_type transform(...);

  static bool const value = sizeof(transform(first1, last1, first2, f)) == sizeof(yes_type);
};

}

int main()
{
  typedef int *iter1;
  typedef const int *iter2;
  typedef std::negate<int> func;

  std::cout << "valid transform compiles: " << …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae type-traits c++-concepts

12
推荐指数
1
解决办法
1290
查看次数

什么是C++技术规范?

Concepts-lite C++(提案N3701)功能未包含在C++ 1y标准中,但据称它将作为技术规范发布.它究竟意味着什么?它会自动成为下一个C++版本的标准功能吗?

c++ c++-concepts c++14

12
推荐指数
1
解决办法
2159
查看次数

如何定义任意 std::vector 满足的概念?

我想要一个concept需要任意向量作为返回类型:

template<typename T>
concept HasVector = requires (T t) {
    { T.vec() } -> std::same_as<std::vector<int>>; //works
    { T.vec() } -> std::same_as<std::vector<foo>>; //want to put something arbitrary in here
}
Run Code Online (Sandbox Code Playgroud)

这样我们就会有如下内容:

class A {
std::vector<int> vec() { /* ... */}
}

class B {
std::vector<double> vec() { /* ... */}
}

static_assert(HasVector<A>);
static_assert(HasVector<B>);
Run Code Online (Sandbox Code Playgroud)

此外,要求一个向量作为返回类型会更好,其值类型满足其他一些概念,即


template<typename T>
concept Arithmetic = // as in the standard

template<typename T>
concept HasArithmeticVector = requires (T t ) {
    { T. vec() } -> …
Run Code Online (Sandbox Code Playgroud)

c++ templates vector c++-concepts c++20

12
推荐指数
2
解决办法
1117
查看次数

需要特定成员返回类型的概念

我在开始了解 C++20 概念时遇到一些困难。我想定义一个概念,要求类有一个名为的成员,count_该成员必须是类型int

\n
#include <concepts>\n\ntemplate <typename T>\nconcept HasCount = requires(T thing) {\n    { thing.count_ } -> std::same_as<int>;\n};\n
Run Code Online (Sandbox Code Playgroud)\n

以下结构应该满足这个概念:

\n
struct BaseTableChunk {\n    BaseTableChunk* next_;\n    int count_ = 0;\n    int data_[1000];\n};\n
Run Code Online (Sandbox Code Playgroud)\n

然后,以下代码无法编译:

\n
template <HasCount Chunk>\nclass BaseTable {\n    void doSomething();\n};\n\nint main() {\n    BaseTable<BaseTableChunk> table{};\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

编译器给出以下错误:

\n
note: constraints not satisfied\nIn file included from /usr/include/c++/10/compare:39,\n                 from /usr/include/c++/10/bits/stl_pair.h:65,\n                 from /usr/include/c++/10/bits/stl_algobase.h:64,\n                 from /usr/include/c++/10/bits/char_traits.h:39,\n                 from /usr/include/c++/10/ios:40,\n                 from /usr/include/c++/10/ostream:38,\n                 from /usr/include/c++/10/iostream:39,\n                 from Minimal2.cxx:1:\n/usr/include/c++/10/concepts:57:15:   required for the …
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++20

12
推荐指数
1
解决办法
1277
查看次数

C++20 使用概念来限制“自动”非类型模板参数?

我有一个模板类,它采用几个非类型参数(GPIO 描述符)。Auto 用于允许使用多种不同的 GPIO 实现。这些实现不能从公共基类派生。

我可以使用一个概念将这些参数限制为具有正确接口的参数吗?

我有一个概念,但它不起作用,GCC 告诉我“错误:‘DigitalOutput’不限制类型”:

template <auto gp>
concept DigitalOutput = requires() { gp.set(); gp.reset(); };

// template <auto output>
template <DigitalOutput output>
struct driver {...};
Run Code Online (Sandbox Code Playgroud)

有可能使这项工作成功吗?

c++ templates c++-concepts c++20

12
推荐指数
3
解决办法
765
查看次数

为什么C++没有std::invocable_r概念?

C++20 添加了概念,标准库中包含了相当多的概念。有一个概念特别引起了我的注意:std::invocable,它验证可以使用一组参数来调用函子。

std::invocable只是 的语法糖std::is_invocable。然而,标准库进一步定义了std::is_invocable_r哪些测试函子是否可以使用一组参数调用,而且一旦调用它就返回特定类型。nothrow这两个实用程序也有不同的版本。然而,标准中没有定义等效的概念。

该标准没有定义这些概念是否有原因,或者只是一个疏忽?是否有一些普通读者没有注意到的细节导致委员会决定不包括这些内容?

c++ language-design c++-concepts c++20

12
推荐指数
1
解决办法
1460
查看次数

使用概念具有特定值类型的任何容器的 C++ 迭代器

我想摆脱enable_if模板中的所有邪恶s 并用 C++20 概念替换它们,但是几乎没有关于概念的任何信息,并且几乎我阅读的任何来源的语法都发生了变化。

这是一个函数,它接受任何带有MyClass值的容器的两个迭代器:

template <class IteratorType, typename = std::enable_if<std::is_same<
                                typename std::iterator_traits<IteratorType>::value_type,
                                MyClass
                            >::value, void>>
void myFunction( IteratorType begin, IteratorType end ) {}
Run Code Online (Sandbox Code Playgroud)

我知道可以使用概念转换此功能,但我找不到好的线索开始。

c++ sfinae enable-if c++-concepts c++20

11
推荐指数
3
解决办法
514
查看次数