C++ 提供方法指针和派生类型的对象时std::bind的一致性
这个问题基本上是我回答的结果。我刚刚意识到标准中的措辞似乎忽略了一些情况。考虑这段代码:C++ 提供方法指针和派生类型的对象时std::bind的一致性,c++,c++11,language-lawyer,stdbind,C++,C++11,Language Lawyer,Stdbind,这个问题基本上是我回答的结果。我刚刚意识到标准中的措辞似乎忽略了一些情况。考虑这段代码: #include <iostream> #include <functional> struct foo { void f(int v) { std::cout << v << std::endl; } }; struct bar : foo {}; int main() { bar obj; std::bind(&foo::f, o
#include <iostream>
#include <functional>
struct foo
{
void f(int v) { std::cout << v << std::endl; }
};
struct bar : foo {};
int main()
{
bar obj;
std::bind(&foo::f, obj, 1)();
}
#包括
#包括
结构foo
{
void f(int v){std::coutC++11标准的第20.8.9.1.3段定义了调用std::bind()
伪函数的效果,如下所示:
返回:具有弱结果类型(20.8.2)的转发呼叫包装器g
。g(u1,u2,…,uM)
的效果应为INVOKE(fd,std::forward(v1),std::forward(v2),…,std::forward(vN),
结果(类型)
请注意,调用std::forward
将始终返回引用类型,无论是左值引用还是右值引用。根据第20.2.3/1-2段,实际上:
template constepr T&&forward(typename remove\u reference::type&T)无异常;
template constepr T&&forward(typename remove\u reference::type&&T)noexcept;
返回:static\u cast(t)
因此,INVOKE
伪函数(注意:不是bind()
我相信标准本身从来没有将INVOKE()
伪函数(这只是一些内部形式主义)用于从T
派生的类型的对象(而不是对对象的引用)
但是,这并不意味着您不能使用对象调用std::bind()
或其他函数,这些函数的定义依次是invoke()
(不是对对象的引用)从T
派生的类型,这似乎是正确的,谢谢。必须通读20.8.9.1.3/10才能弄清楚V1/V1…VN/VN实际上是什么。这当然不是标准中最简单的部分:)我不确定“T类型的对象”何时案例是需要的,为什么参考案例没有涵盖它。但是,在这个措辞中有一个缺陷,请参见。我应该找出何时/为什么需要它,以及是否需要它作为参考包装