简体   繁体   English

带有 ref 限定符的 std::invoke

[英]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 的指针。

As per cppref :根据cppref

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 模板的类型的重载集初始化

cppref : cppref

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));

Demo演示

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.

相关问题 用cv-qualifiers和ref-qualifiers实现std :: is_function - Implementation of std::is_function with cv-qualifiers and ref-qualifiers 无法通过std :: ref()使用auto&参数调用std :: invoke() - Cannot call std::invoke() with auto& parameter via std::ref() 如何比较参数包中的类型(包括引用限定符)和 std::function 参数的类型 - How to compare types (including ref-qualifiers) in a parameter pack and the types of std::function parameters 在带有 std::ref 的 std::thread 中使用地址清理调用 std::invoke(std::forward(...)) 时的奇怪行为 - Strange behavior when calling std::invoke(std::forward(…)) with address-sanitization in a std::thread with a std::ref 使用ref限定符的过载分辨率 - Overload resolution with ref-qualifiers 将const std :: auto_ptr &lt;&gt;作为此std :: auto_ptr &lt;_Tp&gt; :: operator std :: auto_ptr_ref &lt;_Tp1&gt;()的参数传递 - passing const std::auto_ptr<> as this argument of std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() discards qualifiers std::decay 和删除 const 限定符 - std::decay and removing const qualifiers 转发引用,引用限定符和模板成员函数 - Forwarding references, ref qualifiers and template member functions STL容器的右值ref限定词 - rvalue ref-qualifiers for STL containers 简化 c++20 中 ref 限定符的使用 - Simplifying the use of ref qualifiers in c++20
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM