gpt4 book ai didi

c++ - 提供方法指针和派生类型对象时 std::bind 的一致性

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:49 24 4
gpt4 key购买 nike

这个问题基本上是 this 的后果我给的答案。我刚刚意识到标准中的措辞似乎省略了一些情况。考虑这段代码:

#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)();
}

该标准在 20.8.9.1.2 中描述了 std::bind 的效果以及调用它时发生的情况。转发到20.8.2,相关部分是:

20.8.2 Requirements [func.require]

1 Define INVOKE(f, t1, t2, ..., tN) as follows:

(t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T;

((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous item;

t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T;

(*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types described in the previous item;

f(t1, t2, ..., tN) in all other cases.

读到这里,第一个列表项似乎允许三种情况:

  • t1T
  • 类型的对象
  • 或对T类型对象的引用
  • 或对派生自 T
  • 的类型的对象的引用

但在我的示例中,两者都不是。它是从 T 派生的类型,但没有引用。上面列出的情况不应该是:

  • t1T
  • 类型的对象
  • 或派生自 T
  • 的类型的对象
  • 或对T类型对象的引用
  • 或对派生自 T
  • 的类型的对象的引用

当然,20.8.2中的第三个列表项也是如此。

问题 1:由于 GCC 和 Clang 都接受我的代码,我想知道这是一个不符合标准的缺陷报告,还是我只是读错了。

问题 2: 对于多重/虚拟继承,即使类型派生自 T,也可能无法简单地调用 (t1.* f)(...) 就可以了,对吧?这是否也是我应该关注的事情,或者标准是否在给定上下文中明确定义了“派生自”?

最佳答案

C++11 标准的第 20.8.9.1.3 段定义了调用 std::bind() 的效果在 INVOKE() 方面伪函数这样:

Returns: A forwarding call wrapper g with a weak result type (20.8.2). The effect of g(u1, u2, ..., uM) shall be INVOKE (fd, std::forward<V1>(v1), std::forward<V2>(v2), ..., std::forward<VN>(vN),
result_of<FD cv & (V1, V2, ..., VN)>::type)

注意调用std::forward<>将始终返回一个引用类型,无论是左值引用还是右值引用。根据第 20.2.3/1-2 段,事实上:

template <class T> constexpr T&& forward(typename remove_reference<T>::type& t) noexcept;

template <class T> constexpr T&& forward(typename remove_reference<T>::type&& t) noexcept;

Returns: static_cast<T&&>(t)

因此,INVOKE伪函数(注意:不是 bind() 函数!)将在此处使用引用进行调用,并且您引用的定义支持这一点。

我相信标准本身从不使用 INVOKE()带有从 T 派生的类型的对象(不是对对象的引用)的伪函数(这只是一些内部形式) .

但是,这并不意味着您不能调用 std::bind()或其他函数,其定义又根据 INVOKE() 给出使用派生自 T 的类型的对象(不是对对象的引用) .

关于c++ - 提供方法指针和派生类型对象时 std::bind 的一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15310989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com