YSC*_*YSC 6 c++ g++ c++-concepts c++20
In this concept definition:
#include <utility>
template<class Func, class Ret, class... Args>
concept Invokable = requires(Func f) {
{ f(std::declval<Args>()...) } -> Ret;
};
Run Code Online (Sandbox Code Playgroud)
when instantiated like this:
static_assert(Invokable<decltype([](int){}), void, int>);
Run Code Online (Sandbox Code Playgroud)
gcc-9.0.1 (trunk) dumps (well, the Standard Library implementation to be precise):
Run Code Online (Sandbox Code Playgroud)$ g++ -O2 -std=c++2a -fconcepts -Wall -Wextra -Werror -c tu1.cpp error: static assertion failed: declval() must not be used! 2204 | static_assert(__declval_protector<_Tp>::__stop, | ^~~~~~
demo: https://godbolt.org/z/D0ygU4
Is it wrong to reject this code? If not, what did I do wrong? If yes, where should one report this bug?
template<auto f, class... Args>
constexpr auto size_of_return_type = sizeof(f(std::declval<Args>()...));
Run Code Online (Sandbox Code Playgroud)
when instantiated like so:
static_assert(sizeof(int) == size_of_return_type<[](int){ return 0; }, int>);
Run Code Online (Sandbox Code Playgroud)
Demo: https://godbolt.org/z/gYGk8U
[expr.prim.req]/2A requires-expression is a prvalue of type bool whose value is described below. Expressions appearing within a requirement-body are unevaluated operands.
Bar*_*rry 12
Is it wrong to reject this code?
Yes, concepts are never evaluated, as evidenced in the quote you're citing. This is gcc bug 68781 and gcc bug 82171.
请注意,没有理由declval在概念内使用。这更简单:
template<class Func, class Ret, class... Args>
concept InvokableR = requires(Func&& f, Args&&... args) {
{ f(std::forward<Args>(args)...) } -> Ret;
};
Run Code Online (Sandbox Code Playgroud)
declval之所以存在,是因为您需要某种类型的表达式,而不能仅仅T()因为需要默认的构造函数而编写代码。概念使您具备一流的语言功能。仍然需要forward。
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |