[英]std::invoke with ref qualifiers
I'm running into the following issue when using ref qualifiers with operator()
below.在下面使用带有
operator()
的 ref 限定符时,我遇到了以下问题。 What is the correct syntax to enable the l-value ref overload in this instance?在这种情况下启用左值 ref 重载的正确语法是什么?
#include <functional>
struct F {
void operator()() & {}
void operator()() && {} // Commenting this overload enables code to compile
};
int main() {
F f;
std::invoke(&F::operator(), f);
}
Error错误
<source>: In function 'int main()':
<source>:10:15: error: no matching function for call to 'invoke(<unresolved overloaded function type>, F&)'
10 | std::invoke(&F::operator(), f);
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
In file included from <source>:1:
/opt/compiler-explorer/gcc-trunk-20220731/include/c++/13.0.0/functional:107:5: note: candidate: 'template<class _Callable, class ... _Args> constexpr std::invoke_result_t<_Fn, _Args ...> std::invoke(_Callable&&, _Args&& ...)'
107 | invoke(_Callable&& __fn, _Args&&... __args)
| ^~~~~~
/opt/compiler-explorer/gcc-trunk-20220731/include/c++/13.0.0/functional:107:5: note: template argument deduction/substitution failed:
<source>:10:15: note: couldn't deduce template parameter '_Callable'
10 | std::invoke(&F::operator(), f);
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:10:15: error: no matching function for call to 'invoke(<unresolved overloaded function type>, F&)'
10 | std::invoke(&F::operator(), f);
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
In file included from <source>:1:
/opt/compiler-explorer/gcc-trunk-20220731/include/c++/13.0.0/functional:107:5: note: candidate: 'template<class _Callable, class ... _Args> constexpr std::invoke_result_t<_Fn, _Args ...> std::invoke(_Callable&&, _Args&& ...)'
107 | invoke(_Callable&& __fn, _Args&&... __args)
| ^~~~~~
/opt/compiler-explorer/gcc-trunk-20220731/include/c++/13.0.0/functional:107:5: note: template argument deduction/substitution failed:
<source>:10:15: note: couldn't deduce template parameter '_Callable'
10 | std::invoke(&F::operator(), f);
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
Execution build compiler returned: 1
For the first argument of std::invoke
, which is expected to be a pointer to a member function.对于
std::invoke
的第一个参数,它应该是指向成员 function 的指针。
A pointer to function may be initialized from an overload set which may include functions, function template specializations, and function templates, if only one overload matches the type of the pointer
指向 function 的指针可以从可能包含函数、function 模板特化和 function 模板的类型的重载集初始化
The parameter types and the return type of the function must match the target exactly, no implicit conversions are considered
function 的参数类型和返回类型必须完全匹配目标,不考虑隐式转换
In your case, &F::operator()
can't be deduced, you need to provide the type explicitly.在您的情况下,
&F::operator()
无法推断,您需要明确提供类型。
std::invoke<void(F::*)()&>(&F::operator(), f);
std::invoke<void(F::*)()&&>(&F::operator(), std::move(f));
Along the lines of the answer provided by @Nimrod, the following explicit cast works as well.根据@Nimrod 提供的答案,以下显式转换也有效。
int main() {
F f;
std::invoke(static_cast<void(F::*)()&>(&F::operator()), f);
std::invoke(static_cast<void(F::*)()&&>(&F::operator()), std::move(f));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.