小编Moh*_*hit的帖子

使用概念的部分专业化

我只是在阅读 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)

c++ partial-specialization c++-concepts c++20

2
推荐指数
2
解决办法
769
查看次数

标签 统计

c++ ×1

c++-concepts ×1

c++20 ×1

partial-specialization ×1