Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可变宏编译错误_C++ - Fatal编程技术网

C++ 可变宏编译错误

C++ 可变宏编译错误,c++,C++,我想学习如何使用宏 我只是编写了一个示例,但未能在本地g++4.9上编译 #define P(...) printf("13", ##__VA_ARGS__) int main() { // your code goes here P(); return 0; } 我将得到如下编译错误 g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp: In function 'int main()

我想学习如何使用宏

我只是编写了一个示例,但未能在本地g++4.9上编译

#define P(...) printf("13", ##__VA_ARGS__)
int main() {
// your code goes here
P();
return 0;
}
我将得到如下编译错误

g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In function 'int main()':
main.cpp:4:42: error: expected primary-expression before ')' token
 #define P(...) printf("13", ##__VA_ARGS__)
                                          ^
main.cpp:7:5: note: in expansion of macro 'P'
     P();
     ^
但同样的代码可以在ideone上编译。。。。

到2015年

对此有什么合理的解释吗

如何为所有编译器编写可移植宏


在C++中,使用变量参数的函数需要一个原型声明,而在C++中,所有函数都需要一个原型。
printf
的声明可在
stdio.h
中找到

#include <stdio.h>
#define P(...) printf("13", ##__VA_ARGS__)
int main() {
P();
return 0;
}
#包括
#定义P(…)printf(“13”,###uu VA_uargs_uu)
int main(){
P();
返回0;
}
##uu VA_uargs_u
语法是非标准的。它是一个由GCC实现的“如果
\uuuu VA\u ARGS\uuuu
为空,则为空”扩展,似乎已被其他编译器采用

关于
-std=c++14
的行为:

编译器可以接受多个基本标准,如“c90”或“c++98”,以及这些标准的GNU方言,如“gnu90”或“GNU++98”。当指定了一个基本标准时,编译器会接受所有遵循该标准的程序,以及那些使用GNU扩展的程序,这些程序与该标准没有冲突。例如,-std=c90关闭与ISO c90不兼容的GCC的某些功能,例如asm和typeof关键字,但不关闭其他在ISO c90中没有意义的GNU扩展,例如省略?:表达式的中间项。

##uu VA_uargs_u
扩展与标准不冲突。导致coliru网站拒绝的原因是设置了
-pedantic
标志

<>有效的ISO和ISO C++程序应在有或没有这个选项的情况下正确编译(虽然很少有需要ANSI或-STD选项指定ISO C的要求版本)。但是,如果没有这个选项,也会支持某些GNU扩展和传统的C和C++特性。使用此选项,它们将被拒绝。


代码不一样-在coliru stdio.h中不包括-iostream是…但通过
-std=c++14
或任何标准都会删除此扩展,即使在gcc中也是如此,这就是为什么coliru示例没有删除此扩展的原因compile@onqtam:这不是
-std
所做的。使用STD=GNU+11来重新启用GNU扩展。简单地说,似乎没有办法用可变宏来编写一个可移植的C++代码。我需要使用STD=C++ 14或11,因为我使用了一些可变模板。我将尝试使用-std=gnu++11作为变通方法