c++ - 如何在 `requires` 约束中声明表达式的泛型类型?
问题描述
我可能在这里问了一个错误的问题,但是我到底做错了什么导致编译器认为我对堆栈的 pop 方法所期望的约束是std::same_as<void, T>
?
#include <concepts>
#include <stack>
template <typename S, typename T>
concept generic_stack = requires(S s, T t) {
s.push(t);
{ s.pop() } -> std::same_as<T>; // error seems to stem from here
};
template <typename T, generic_stack<T> S>
void compile_if_stack(S) {}
int main() {
compile_if_stack<int>(std::stack<int>{});
}
我试过std::same_as<decltype(s.pop()), T>;
了,它似乎有效,但我不明白前一种方法有什么问题。
完全错误
# clang 12.0.1
$ clang++ -std=c++20 main.cpp
main.cpp:14:5: error: no matching function for call to 'compile_if_stack'
compile_if_stack<int>(std::stack<int>{});
^~~~~~~~~~~~~~~~~~~~~
main.cpp:11:6: note: candidate template ignored: constraints not satisfied [with T = int, S = std::stack<int>]
void compile_if_stack(S) {}
^
main.cpp:10:23: note: because 'generic_stack<std::stack<int>, int>' evaluated to false
template <typename T, generic_stack<T> S>
^
main.cpp:7:25: note: because type constraint 'std::same_as<void, int>' was not satisfied:
{ s.pop() } -> std::same_as<T>; // error seems to stem from here
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/concepts:63:19: note: because '__detail::__same_as<void, int>' evaluated to false
= __detail::__same_as<_Tp, _Up> && __detail::__same_as<_Up, _Tp>;
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/concepts:57:27: note: because 'std::is_same_v<void, int>' evaluated to false
concept __same_as = std::is_same_v<_Tp, _Up>;
^
1 error generated.
C++ 编译器GCC 11.1.0
提出了语义相同的错误消息。
解决方案
这是因为stack.pop()
退货void
,如std::stack::pop
.
约束不正确,您应该检查top
:
template <typename S, typename T>
concept generic_stack = requires(S s, T t) {
s.push(t);
{ s.top() } -> std::same_as<T const&>;
};
推荐阅读
- swift - 如何在初始化期间将本地函数传递给另一个对象?
- c++ - 函数模板中的模板模板参数问题
- android-management-api - 无法使用企业帐户发布自托管私有应用程序
- flutter - 如何在 Flutter 中实现这个时间选择器 UI?
- android-studio - 为什么在 android studio 更新后出现“manifestOutputDirectory”错误?
- python - Python - PySimpleGUI - 无法选择日历输入
- java - 我有这个错误:“无法将 java.lang.String 类型的对象转换为 com.flashpub.flash.imageSection 类型”,我不知道为什么
- javascript - 如何使用 Electron.js 彼此相邻显示自定义 Windows 消息框按钮
- android - 我怎样才能在android中制作一个完美的正方形?
- ios - 如何在运行时更改 UIMenu 内的 UIAction 的状态?