我只是在阅读 C++20 概念的例子。现在我正在尝试创建一个函数,该函数将打印出给定类型是否是哈希表或不使用与部分专业化混合的概念。但不幸的是它不起作用。
#include <iostream>
#include <string>
template<typename T>
concept Hashtable = requires(T a) {
{ std::hash<T>{}(a) } -> std::size_t;
};
struct Foo {};
template <typename T>
void Bar() {
std::cout << "Type T is not a hashtable" << std::endl;
}
template <Hashtable T>
void Bar<T> {
std::cout << "Type T is a hashtable" << std::endl;
}
int main()
{
Bar<Foo>();
Bar<std::string>();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用编译器版本 GCC HEAD 9.0.1,编译器标志是g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.69.0/gcc-head/include -std=gnu++2a "-fconcepts". 它给了我以下编译器错误:
prog.cc:18:6: error: template-id 'Bar<T>' …Run Code Online (Sandbox Code Playgroud)