g ++ 9概念支持包括< concepts>在Ubuntu 18.04上 [英] g++ 9 concepts support include <concepts> on ubuntu 18.04

查看:86
本文介绍了g ++ 9概念支持包括< concepts>在Ubuntu 18.04上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有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
}

解决方案

GCC 9不支持C ++ 20概念,仅支持与以前的概念技术规范(TS)不同的早期概念技术规范(TS)(例如,定义概念的语法)不一样.

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

Concepts TS已记录在此cppreference页面上的 不是 您从中获得此代码的.

I am playing with g++ with concepts using g++ -std=c++2a -fconcepts but getting an error with #include concepts header: no such file or directory. Can someone please help me debug this. Here is the my code I copied from 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 does not support C++20 concepts, only the earlier Concepts Technical Specification (TS), which differs from the former (e.g. the syntax to define concepts is different).

You need GCC 10 to use this code.

The Concepts TS is documented on this cppreference page, not this one from which you have the code.

这篇关于g ++ 9概念支持包括&lt; concepts&gt;在Ubuntu 18.04上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆