无效使用非静态成员函数在实例化类模板的成员函数时? [英] Invalid use of non-static member function In instantiation of member function of a class template?

查看:143
本文介绍了无效使用非静态成员函数在实例化类模板的成员函数时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望一个类模板启动一些线程来测试一些访问某些共享状态的函数。

 #包括< vector> 
#include< thread>
使用命名空间std;

template< std :: size_t M,std :: size_t N>
A类{
公共:
无效测试(std :: size_t n){
std :: vector< std :: thread> ts;
for(int i = 0; i< N; ++ i){
ts.push_back(
std :: thread(A :: foo,this,i,n)
);
}
for(auto& thread:ts){
thread.join();
}
}

私人:
void foo(std :: size_t tid,std :: size_t n){
}
} ;

int main(){
A< 10,2>测试员
tester.test(1000);
}

这会产生以下错误。为什么以及如何解决?

  prog.cpp:以'void A< M,N> :: test(std ::: size_t)[unsigned int M = 10u;无符号整数N = 2u; std :: size_t = unsigned int]':
prog.cpp:27:18:从此处需要
prog.cpp:11:27:错误:无效使用非静态成员函数
threads.push_back(

编辑:



它按照@Igor的建议更改为 std :: thread(& A :: foo,this,i,n)后进行编译。据我了解,函数名称在传递给函数时会衰减为指针。为什么我们仍然需要使用&号?

解决方案

[expr.prim.general] / 13 只能使用 id-expression 表示类的非静态数据成员或非静态成员函数:



(13.1)—作为类成员访问(5.2.5)的一部分,其中对象表达式引用成员的类或从该类派生的类,或者



(13.2)—至f orm指向成员(5.3.1)的指针,或者



(13.3)-如果该 id-expression 表示一个非静态数据成员


进一步:


[expr.ref] / 4 ...适用以下规则之一。



(4.3.2) —否则,如果 E1.E2 引用一个非静态成员
函数,则该表达式只能用作左操作数成员函数调用中的
(9.3)...


因此,基本上, A :: foo 只能合法地在前面的&符号(以形成指向成员的指针)或后面的开头括号(函数调用的左侧操作数)中出现。


I want a class template to start some number of threads to test some functions, which access some shared states.

#include <vector>
#include <thread>
using namespace std;

template<std::size_t M, std::size_t N>
class A {
public:
    void test(std::size_t n) {
        std::vector<std::thread> ts;
        for(int i = 0; i < N; ++i){
            ts.push_back(
                    std::thread( A::foo, this, i, n )
            );
        }
        for(auto& thread : ts){
            thread.join();
        }
    }

private:
    void foo( std::size_t tid, std::size_t n ) {
    }
};

int main() {
    A<10, 2> tester;
    tester.test(1000);
}

This gives following error. Why and how to fix?

prog.cpp: In instantiation of 'void A<M, N>::test(std::size_t) [with unsigned int M = 10u; unsigned int N = 2u; std::size_t = unsigned int]':
prog.cpp:27:18:   required from here
prog.cpp:11:27: error: invalid use of non-static member function
          threads.push_back(

Edit:

It compiles after changing to std::thread( &A::foo, this, i, n ) as @Igor suggested. As far as I understand, function name decay to a pointer when passing into a function. Why do we still need the ampersand '&'?

解决方案

Re: why the ampersand is required. Because the standard says so.

[expr.prim.general]/13 An id-expression that denotes a non-static data member or non-static member function of a class can only be used:

(13.1) — as part of a class member access (5.2.5) in which the object expression refers to the member's class or a class derived from that class, or

(13.2) — to form a pointer to member (5.3.1), or

(13.3) — if that id-expression denotes a non-static data member and it appears in an unevaluated operand.

And further:

[expr.ref]/4 ...one of the following rules applies.

(4.3.2) — Otherwise, if E1.E2 refers to a non-static member function... [t]he expression can be used only as the left-hand operand of a member function call (9.3)...

So basically, A::foo can legally appear only with the ampersand in front ("to form a pointer to member") or with the opening paren following ("left-hand operand of a function call").

这篇关于无效使用非静态成员函数在实例化类模板的成员函数时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆