c++ - 带有指向数据成员的指针的 std::thread

标签 c++ c++11 std

我正在阅读 std::thread documentation at cppreference (并不总是 100% 准确,我知道)并注意到当传递“指向数据成员的指针”(不是“指向成员的指针”)时 std::thread 的行为的以下定义-function") 作为它的第一个参数 (f) 和一个所需类的对象作为它的第二个参数 (t1 在复制到 thread-local-storage 之后):

If N == 1 and f is pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed: t1.*f if and the type of t1 is either T, reference to T or reference to type derived from T. (*t1).*f otherwise.

现在,我不打算以这种方式使用 std::thread,但我对这个定义感到困惑。显然,唯一发生的事情是数据成员被访问并且值被忽略,这似乎根本没有任何可观察到的副作用,这意味着(据我所知)它也可能是一个没有操作。 (我可能遗漏了一些明显的东西......?)

起初,我认为这可能是一个打印错误,意思是说数据成员被访问然后被调用(因为它可能是一个可调用对象,即使它不是一个函数)但我用下面的测试GCC-4.7 中的代码,确实没有调用:

#include <iostream>
#include <thread>

struct S 
{
    void f() {
        std::cout << "Calling f()" << std::endl;
    }

    struct {
        void operator()() {
            std::cout << "Calling g()" << std::endl;
        }
    } g;
};

int main(int, char**)
{
    S s;
    s.f(); // prints "Calling f()"
    s.g(); // prints "Calling g()"

    std::cout << "----" << std::endl;

    auto x = &S::f; // ptr-to-mem-func
    auto y = &S::g; // ptr-to-data-mem

    (s.*x)(); // prints "Calling f()"
    (s.*y)(); // prints "Calling g()"

    std::cout << "----" << std::endl;

    std::thread t(x, &s);
    t.join();
    // "Calling f()" printed by now
    std::thread u(y, &s);
    u.join();
    // "Calling g()" not printed

    return 0;
}

这个定义似乎没有完成任何事情的目的是什么?为什么不让传递“指向数据成员可调用的指针”就像传递“指向成员函数的指针”一样,并使传递“指向数据成员不可调用的指针”成为错误?事实上,这似乎是实现它的最简单方法,因为在其他上下文中调用“指向数据成员的指针”具有等效于调用“指向成员函数的指针”的语法(除非模板特化和 SFINAE 规则的变化莫测,这使得很难将它们同等对待......?)

这不是我实际代码所需要的东西,但这个定义存在的事实让我怀疑我遗漏了一些基本的东西,这让我很担心......任何人都可以启发我吗?

最佳答案

这是因为通用绑定(bind)工具,C++11 标准不仅定义了线程的启动方式,还定义了如何启动 std::bind。和 std::function工作。

事实上,C++11 标准的第 30.3.1.2/3 段指定了类 std::thread 的可变参数构造函数。 :

template <class F, class ...Args> explicit thread(F&& f, Args&&... args);

Effects: Constructs an object of type thread. The new thread of execution executes INVOKE (DECAY_- COPY ( std::forward<F>(f)), DECAY_COPY (std::forward<Args>(args))...) with the calls to DECAY_COPY being evaluated in the constructing thread. Any return value from this invocation is ignored. [...]

忽略什么DECAY_COPY确实(与问题无关),这就是第 20.8.2 段定义 INVOKE 的方式伪函数:

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.

现在问题变成了:

Why does the C++11 Standard defines the INVOKE facility that way?

答案是 here .

关于c++ - 带有指向数据成员的指针的 std::thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080015/

相关文章:

c++ - 如何从 C++ 中的 tm 对象获取星期几?

c++ - 访问 map<int,string[4]> 中的元素

c++11 - 是否可以覆盖 C++11 lambda/closure 析构函数?

c++ - 长正则表达式会导致错误

c++ - 当在函数调用中使用 new 时,你释放了什么?如下所示在 boost 调用中

c++ - 最快的插入 C++

c++ - 在基本类型上使用 std::declval

c++ - std::tr1::function 如何获取签名特征的参数并特化类*

c++ - 可以获得对象的静态类型吗?

c++ - cudaMemcpyDeviceToHost() 失败