簡體   English   中英

為什么 std::is_invocable <std::decay_t<void(int&)> , 標准::decay_t<int> &gt;::值是假的? </int></std::decay_t<void(int&)>

[英]Why std::is_invocable<std::decay_t<void(int&)>, std::decay_t<int>>::value is false?

std::is_invocable<std::decay_t<void(int&)>, std::decay_t<int>>::value

評估為假。

void(int&)衰減為void*(int&)

intint

我可以像這樣使用std::invoke

void f(int&);
...
auto* fp = f;
int i = 0;
std::invoke(fp, i);

當我查看std::thread構造函數時,我碰到了這個:

 template<typename _Callable, typename... _Args,
             typename = _Require<__not_same<_Callable>>>
      explicit
      thread(_Callable&& __f, _Args&&... __args)
      {
        static_assert( __is_invocable<typename decay<_Callable>::type,
                                      typename decay<_Args>::type...>::value,
          "std::thread arguments must be invocable after conversion to rvalues"
          );

我理解為什么不鼓勵傳遞對std::thread的構造函數的引用(我們仍然可以使用std::ref() )但我不明白為什么void *(int&)不能用int調用。

std::is_invocable<..., int>嘗試使用int右值作為參數。 它相當於std::is_invocable<..., int &&>

使用std::is_invocable<..., int &>


std::is_invocable

...正式確定INVOKE(declval<Fn>(), declval<ArgTypes>()...)是否格式正確...

即使我們忽略INVOKE是什么,您也可以看到is_invocable是使用std::declval<ArgType>()定義的,它返回ArgType &&

ArgType &&是一個右值引用。 除非ArgType是左值引用,在這種情況下ArgType &&等效於ArgType並且是左值引用(根據引用折疊規則)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM