Reputation: 69
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
}
Upvotes: 3
Views: 3526
Reputation: 22152
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.
Upvotes: 7