我对函数声明的理解一直是默认值参数在非默认值参数之后声明。
但我刚刚注意到我可以 typedef 一个违反此规则的函数声明:
typedef // type of "int my_function ( int=1 , int )"
int // return type
( t_func_ptr ) // function type name
( int = 1 // arg0, default value - declared before non-default
, int // arg1
) ;
/*
// this wont compile, so why be able to typedef it?
int my_bad_function ( int=1 , int )
{
} ;
*/
我很好奇为什么 typedef 是可能的?
编辑:我使用一系列编译器进行编译(我使用的是跨目标IDE),不确定底层编译器版本,需要检查,但目标适用于Windows(mingw),linux ubuntu,avr, arduino(atmega、due、uno)、raspberrypi、microchip(chipkit)等– 迈克尔·科利尔 2 小时前
i686-w64-mingw32-g++ (GCC) 4.8.2
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
avr-g++ (GCC) 4.8.1
arm-none-eabi-g++(用于 ARM 嵌入式处理器的 GNU 工具) 4.8.3 20140228(发布)[ARM/embedded-4_8-branch 修订版 208322]
用于 PIC32 MCU v1.31-20120614 的 pic32-g++ (chipKIT) 4.5.1 ChipKIT 编译器
arm-none-eabi-g++(适用于 ARM 嵌入式处理器的 GNU 工具)4.7.4 20130913(发布)[ARM/embedded-4_7-branch 修订版 202601]
msp430-g++(MSPGCC 20120406(包含补丁:sf3540953 sf3559978))
4.6.3 20120301(mspgcc LTS 20120406 未修补)
arm-linux-gnueabihf-g++(crosstool-NG 1.17.0)4.7.2
最佳答案
仅允许在函数声明和 lambda 表达式的参数列表中使用默认参数 (C++14 起),而不允许在函数指针声明、函数引用或 typedef 声明中使用默认参数。/strong> (例如,参见 http://en.cppreference.com/w/cpp/language/default_arguments )。
g++ 在声明接受默认函数参数的函数指针类型/非指针函数时遇到问题(请参阅 bug 28262 和 g++ v4.7.3 、 g++ v4.8.1 )。
该错误已为 fixed in version 4.9.0 .
关于c++ - 函数 typedef 声明符 - 默认参数位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244798/