如何通过标准参数传递std :: move

问题描述 投票:1回答:1

[通常,我会尝试在&&类型中使用一个模板化函数。问题是,如果我将std :: move作为参数,则会出现诸如此类的错误:

error: no matching function for call to 'doThing(void (*)(int&&), std::remove_reference<int&>::type)'

生成该特定错误的代码如下:

#include <utility>

template<typename T>
void doThing(void (*thing)(T), T input)
{
    thing(input);
}

void exampleThing(int&& someData)
{someData++;}

int main()
{
    int x, y = 5;
    exampleThing(std::move(x));             //compiles fine
    doThing(&exampleThing, std::move(y));   //error as shown above
}

所以,我如何将参数作为模板传递给模板?

c++ templates types move
1个回答
1
投票

问题与

template<typename T> void doThing(void (*thing)(T), T input)

T是从2个地方推导出的,应该相同。

简单分割成两个模板参数:

template <typename Arg, typename T>
void doThing(void (*thing)(Arg), T&& input)
{
    thing(std::forward<T>(input));
}
© www.soinside.com 2019 - 2024. All rights reserved.