简体   繁体   English

参数包和函数声明

[英]Parameter packs and function declaration

I have three files - lib.h with function declarations, lib.cpp with implementation and main.cpp entry point. 我有三个文件- lib.h与函数声明, lib.cpp与实施和main.cpp切入点。 Their contents is just as simple as: 它们的内容非常简单:

//lib.h
#pragma once

void func1();
void func2();
void funcN();

//lib.cpp
#include "lib.h"

void func1(){}
void func2(){}
void funcN(){}

//main.cpp
#include "lib.h"

int main() {
    return 0;
}

I compile it like so: 我这样编译它:

$ g++ main.cpp lib.cpp

So far, so good. 到现在为止还挺好。 But now I want to use parameter pack in one of my functions. 但是现在我想在我的函数之一中使用参数包。 Like so: 像这样:

//change in lib.cpp

void funcN(int i, auto... j) {

}

I change lib.h and main.cpp respectively: 我分别更改lib.h和main.cpp:

//change in lib.h
void funcN(int, auto...);

//change in main.cpp

int main() {
    funcN(1, 2);
    return 0;
}

But now, when I compile it with 但是现在,当我用

$ g++ main.cpp lib.cpp

I get this error message: 我收到此错误消息:

main.cpp:(.text+0x14): undefined reference to `void funcN(int, int, ...)' collect2: error: ld returned 1 exit status main.cpp :(。text + 0x14):对`void funcN(int,int,...)'的未定义引用collect2:错误:ld返回1退出状态

I know that this error is because of auto... and I know that I can probably solve it, if I put implementation inside lib.h , but this looks nasty - to have some implementations in one file and other implementations in another. 我知道这个错误是由于auto...而且我知道我可以解决这个问题,如果我将实现放在lib.h ,但这看起来很讨厌-在一个文件中包含某些实现,而在另一个文件中包含其他实现。 I would like to know, how they do it in real world practice. 我想知道,他们是如何在现实世界中实践的。

Using auto as a function parameter is not Standard C++. auto用作函数参数不是标准C ++。 Your code is currently not valid C++. 您的代码当前不是有效的C ++。 See Is auto as a parameter in a regular function a GCC 4.9 extension? 请参阅在常规功能中将auto作为参数作为GCC 4.9扩展吗? for more information. 欲获得更多信息。 Note that auto as a function parameter (along with shorthand concept syntax) was not added to C++20's working draft yet. 请注意, auto作为函数参数(以及速记概念语法)尚未添加到C ++ 20的工作草案中。

Regardless, using auto in that manner is just shorthand for function template definition - this means that your need to define your function in the header. 无论如何,以这种方式使用auto只是函数模板定义的简写-这意味着您需要在标头中定义函数。

Here's a solution that's valid C++: 这是有效的C ++解决方案:

// lib.h
template <typename... Js>
void funcN(int i, Js... js) {

}

More information: Why can templates only be implemented in the header file? 详细信息: 为什么只能在头文件中实现模板?

In this case auto... does not declare parameter pack with auto type. 在这种情况下, auto...不会用自动类型声明参数包。 It declares an unnamed parameter followed by regular ellipsis because compiler allows to omit comma before elipses. 它声明一个未命名的参数,后跟常规的省略号,因为编译器允许在省略号之前省略逗号。 Declare it without auto if you want to get a regular variadic function 如果要获得常规可变参数功能,则无需auto声明

void funcN(int i, ...)

Or as a proper template in header file if you want to get real parameter pack 或作为头文件中的适当模板(如果要获取实际参数包)

template<typename... TArgs> void
function(int i, TArgs... args) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM