g ++ 9个概念支持包括 在ubuntu 18.04上

问题描述 投票:0回答:1

我正在使用带有g ++ -std = c ++ 2a -fconcepts的概念的g ++,但是#include concepts标头出现错误:没有这样的文件或目录。有人可以帮我调试一下吗。这是我从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
}

c++ g++
1个回答
1
投票

[GCC 9不支持C ++ 20概念,仅支持较早的概念技术规范(TS),与以前的概念有所不同(例如,定义概念的语法不同)。

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

Concepts TS已记录为on this cppreference page不是 this one,您可以从中获得代码。

© www.soinside.com 2019 - 2024. All rights reserved.