[英]Passing variadic function template arguments to another function
I am trying to pass my pack to another function, it looks similar to what I'm seeing in tutorials, but not similar enough apparently: 我试图将我的包传递给另一个函数,它看起来与我在教程中看到的相似,但显然不够相似:
class algorithm
{
typedef uint64_t time_type;
protected:
std::string name;
time_type result;
template <typename... Args>
void execute(const Args&...);
public:
algorithm(std::string name);
//virtual void prepareTest()=0;
template <typename TimeT, typename... Args>
void beginTest(const Args&...);
void printResult();
time_type getResult();
};
template <typename TimeT, typename... Args>
void algorithm::beginTest(const Args&... args)
{
auto start = chrono::high_resolution_clock::now();
execute(args...);
auto duration = chrono::duration_cast<TimeT>
(chrono::high_resolution_clock::now() - start);
result = duration.count();
}
template <typename... Args>
void execute(const Args&... args)
{
//nothing here yet
}
When I instantiate an algorithm and beginTest(), I get a linker error: 实例化算法和beginTest()时,出现链接器错误:
const int n = 100000;
const int m = 1000;
algortest::algorithm brutus("brutus");
brutus.beginTest<chrono::milliseconds>(n, m);
undefined symbols for architecture x86_64: "void algortest::algorithm::execute(int const&, int const&)", referenced from: void algortest::algorithm::beginTest >, int, int>(int const&, int const&) in main.o ld: symbol(s) not found for architecture x86_64
架构x86_64的未定义符号:“ void algortest :: algorithm :: execute(int const&,int const&)”,引用自:void algortest :: algorithm :: beginTest>,int,int>(int const&,int const&)in main .o ld:架构x86_64找不到符号
What am I am doing wrong? 我做错了什么? Also, is overriding execute() in a derived class of algorithm going to be possible?
另外,在派生算法的类中重写execute()是否可能?
You forgot to prefix the definition of execute
with algorithm::
is all :-) 您忘了在
execute
的定义execute
加上algorithm::
都是:-)
It should read: 它应显示为:
template <typename... Args>
void algorithm::execute(const Args&... args)
{
// ...
}
You might also want to use perfect-forwarding for your arguments instead of forcing them to be passed by reference -- instead of const Args&...
, use Args&&...
and then std::forward<Args>(args)...
when calling the function. 您可能还想对参数使用完全转发,而不是强制通过引用传递它们-代替
const Args&...
,使用Args&&...
然后使用std::forward<Args>(args)...
调用函数时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.