如何理解 void (*&&)() 函数

Mee*_*epo 3 c++ rvalue rvalue-reference

我使用C++ https://cppinsights.io/查看实例化的进度,Function&&和Function之间有一些令人困惑的地方。

我评论了cppinsights生成的代码。

template<typename Function>
void bind(int type, Function&& func)
{
}

/*
// instantiated from the function above:  
template<>
inline void bind<void (*)()>(int type, void (*&&)() func)
{
}
*/

template<typename Function>
void bindtwo(int type, Function func)
{
}

/*
template<>
inline void bindtwo<void (*)()>(int type, void (*func)())
{
}
*/

void test()
{
    std::cout << "test" << std::endl;
}

int main()
{
    bind(1, &test);
    bindtwo(2, test);
}
Run Code Online (Sandbox Code Playgroud)

use*_*570 5

如何理解void (*&&)() func

首先,上面的语法是错误的,因为func应该出现在 the 之后&&,而不是出现在 the 之后,()如下所示。

现在,经过更正(如下所示)后,func是一个指向函数指针的右值引用,该函数不带参数且返回类型为 void

另请注意,语法void (*&&)() func是错误的,正确的语法如下所示:

template<>
//----------------------------------------------vvvv----->note the placement of func has been changed here
inline void bind<void (*)()>(int type, void (*&&func)() )
{
}
Run Code Online (Sandbox Code Playgroud)