c++ - g++ 9 概念支持包括 ubuntu 18.04 上的 <concepts>

标签 c++ g++

我正在使用 g++ -std=c++2a -fconcepts 与 g++ 一起玩概念,但在 #include 概念 header 中出现错误:没有这样的文件或目录。有人可以帮我调试这个吗?这是我从 cppreference 复制的代码:

#include <string>
#include <cstddef>
#include <concepts>
using namespace std::literals;

// Declaration of the concept "Hashable", which is satisfied by
// any type T such that for values a of type T,
// the expression std::hash<T>{}(a) compiles and its result is convertible to std::size_t
template<typename T>
concept Hashable = requires(T a) {
    { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
};

struct meow {};

template<Hashable T>
void f(T); // constrained C++20 function template

// Alternative ways to apply the same constraint:
// template<typename T>
//    requires Hashable<T>
// void f(T); 
// 
// template<typename T>
// void f(T) requires Hashable<T>; 

int main() {
  f("abc"s); // OK, std::string satisfies Hashable
  f(meow{}); // Error: meow does not satisfy Hashable
}

最佳答案

GCC 9 不支持 C++20 概念,只支持较早的概念技术规范 (TS),它与前者不同(例如,定义概念的语法不同)。

您需要 GCC 10 才能使用此代码。

概念 TS 已记录 on this cppreference page , 不是 this one从中获得代码。

关于c++ - g++ 9 概念支持包括 ubuntu 18.04 上的 <concepts>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60515849/

相关文章:

c++ - CUDA:使用 G++ 链接器和 cuda 对象文件创建对象

linker - MinGW 链接器找不到 MPICH2 库

c++ - 这些 MySQL 类如何链接?

c++ - 如何初始化包含opencv :matrices?的二维 vector

C++ 并行编程函数调用

C++ std::transform 副作用

c++ - QThread内存泄漏

c++ - 如何删除没有构造函数的派生类的对象

c++ - 如何使用 -std=c++11 : ‘auto_ptr’ is deprecated 解决 g++ 警告

c++ - 链接器找不到 mathgl 库