首页 > 解决方案 > C ++,使用模板调用成员函数上的线程

问题描述

我想在这些成员函数上调用线程(它们采用完全相同的参数)。请注意,这 ALL 发生在一个名为 Renderer 的类中。


    template <class Pixel>
    void DrawFlatBottom(Pixel& leftPixel, Vec2& leftScreen, Pixel& topPixel, Vec2& topScreen, Pixel& rightPixel, Vec2& rightScreen, Vec4 (*PixelShader)(Pixel& sd)) {

          // DrawFlatBottom implementation

    }

    template <class Pixel>
    void DrawFlatTop(Pixel& leftPixel, Vec2& leftScreen, Pixel& bottomPixel, Vec2& bottomScreen, Pixel& rightPixel, Vec2& rightScreen, Vec4 (*PixelShader)(Pixel& sd)) {

          // DrawFlatTop implementation 

    }

这是我尝试调用线程的方式:

template <class Pixel>
void DrawTriangle(Pixel p1, Pixel p2, Pixel p3, Vec4 (*PixelShader)(Pixel& sd)) {

     std::thread flatBottom(&Renderer::DrawFlatBottom<Pixel>, std::ref(*middlePixel), std::ref(*middleScreen), std::ref(*topPixel), std::ref(*topScreen), std::ref(cutPixel), std::ref(cutScreen), PixelShader);
     std::thread flatTop(&Renderer::DrawFlatTop<Pixel>, std::ref(*middlePixel), std::ref(*middleScreen), std::ref(*bottomPixel), std::ref(*bottomScreen), std::ref(cutPixel), std::ref(cutScreen), PixelShader);

     // rest of DrawTriangle implementation
}

请注意,这些调用本身是在模板化函数中,其模板参数 Pixel 被传递到另一个模板化函数(DrawFlatBottom 和 DrawFlatTop)

但是,这会导致编译错误

Error   C2672   'std::invoke': no matching overloaded function found
Error   C2893   Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'

我认为我传递的所有参数都是正确的(即:它们都是正确的类型)。也许我错误地传递了 PixelShader 函数指针(最后一个参数)。我很确定它一定与模板有关。

标签: c++multithreadingtemplates

解决方案


推荐阅读