首页 > 解决方案 > 如何在 `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提出了语义相同的错误消息。

标签: c++templatesc++20c++-concepts

解决方案


这是因为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&>;
};

推荐阅读