Space 函数调用参数列表 (C++)

Space in Function Call Parameter List (C++)

我在使用的 C++ 项目中遇到了一些语法,但不知道该怎么做。编译器不会抛出任何与此相关的错误:

lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);

请注意函数参数 [我假设的] 之间的空格。

lua_pushstring 的函数定义是

LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);

SWIG_RUNTIME_VERSION#define 等于 "4"

SWIG_TYPE_TABLE_NAME 在以下块中定义:

#ifdef SWIG_TYPE_TABLE
# define SWIG_QUOTE_STRING(x) #x
# define SWIG_EXPAND_AND_QUOTE_STRING(x) SWIG_QUOTE_STRING(x)
# define SWIG_TYPE_TABLE_NAME SWIG_EXPAND_AND_QUOTE_STRING(SWIG_TYPE_TABLE)
#else
# define SWIG_TYPE_TABLE_NAME
#endif

谁能解释一下这是怎么回事?

为了进一步参考,代码用于 swig project on GitHub: luarun.swg:353 and luarun.swg:364

静态字符串连接。 "Hello " "World" 等同于 "Hello World".

常量字符串放在一起

以下代码生成的输出等于参数列表中的所有三个字符串。

#include <iostream>
void f(const char* s) {
    std::cerr << s << std::endl;
}

int main() {
    f("sksksk" "jksjksj" "sjksjks");
}

C++(和 C)会自动连接相邻的字符串文字。所以

std::cout << "Hello " "World" << std::endl;

会输出"Hello World"。不过,这仅适用于 文字 ,不适用于变量:

std::string a = "Hello ", b = "World";
std::string c = a b //error, use a + b

您可以为此目的使用 std::stringoperator+(或 strcat,但如果可以,请避免使用)。

当我们有一个非常长的字符串文字无法放在一行中时,此功能主要有用:

process_string("The quick brown fox jumps over "
               "the lazy dog");

它也可用于预处理指令,如您的示例中所示。