我对函数声明的理解一直是,默认值参数在非默认值参数之后被声明为。
但是,我刚刚注意到,我能够对一个打破此规则的函数声明进行类型化:
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 )
{
} ;
*/我很想知道为什么类型的人是可能的?
编辑:我用一系列编译器编译(我使用的是跨目标IDE),不确定底层编译器版本,需要检查,但目标是windows (mingw)、linux ubuntu、avr、arduino (atmega,need,uno)、raspberrypi、微芯片(chipkit)等。
i686-w64-mingw32-g++ (GCC) 4.8.2g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4avr-g++ (GCC) 4.8.1arm-none-eabi-g++ (GNU Tools for ARM Embedded Processors) 4.8.3 20140228 (release) [ARM/embedded-4_8-branch revision 208322]pic32-g++ (chipKIT) 4.5.1 chipKIT Compiler for PIC32 MCUs v1.31-20120614arm-none-eabi-g++ (GNU Tools for ARM Embedded Processors) 4.7.4 20130913 (release) [ARM/embedded-4_7-branch revision 202601]msp430-g++ (MSPGCC 20120406 (With patches: sf3540953 sf3559978))4.6.3 20120301 (mspgcc LTS 20120406 unpatched)arm-linux-gnueabihf-g++ (crosstool-NG 1.17.0) 4.7.2发布于 2016-09-02 15:37:24
默认参数只允许在函数声明和lambda-表达式的参数列表中使用(自C++14以来),并且在指向函数的指针的声明中、对函数的引用或在typedef声明(例如,参见参数)中不允许。
g++在声明接受默认函数参数的函数指针类型/非指针函数时出现了问题(请参阅bug 28262和g++ v4.7.3,g++ v4.8.1)。
这个bug是修正4.9.0版。
https://stackoverflow.com/questions/39244798
复制相似问题