指向成员函数的std

指向成员函数的std

本文介绍了GCC 4.7.2:指向成员函数的std :: thread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在编写的测试代码时,我发现注释行下面不会在GCC 4.7.2上编译:

  

以粗体显示的句子有效地指定了以下行:

lockquote
std :: thread t(& S :: f,s);

应该编译。因此,这被认为是一个 bug



此外,GCC 4.8.0(测试版)和Clang 3.2也可以进行线上编译。 p>

In writing test code for this question I found that the commented line below does not compile on GCC 4.7.2:

#include <thread>
#include <iostream>

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

int main()
{
    S s;
    // std::thread t(&S::f, s); // does not compile?
    std::thread t(&S::f, &s);
    t.join();
}

But cppreference seems to claim that the "this" argument can be passed equivalently as an object, reference to object, or pointer to object:

I actually think this sounds terrible, and would prefer std::thread only allow either pointer or reference semantics instead of accepting them interchangeably, but given that it seems like it's supposed to, is the above a GCC/libstdc++ bug (or am I misinterpreting cppreference)?

解决方案

Seems like tonight it's GCC Bug Party :-)

Jokes aside, this is most certainly a bug. My answer to the linked question actually contains the proof, but since it is not emphasized, I will repeat it here.

This is how the INVOKE facility, in terms of which the behavior of std::thread's constructor (see the linked answer) is defined in the C++11 Standard

The sentence in bold font effectively specifies that the line:

Should compile. Therefore, this qualifies as a bug.

Besides, that does line compile on GCC 4.8.0 (beta) and Clang 3.2.

这篇关于GCC 4.7.2:指向成员函数的std :: thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:23