function typedef declarator - 默认参数位置

时间:2016-08-31 08:44:08

标签: c++ function parameters typedef default-parameters

我对函数声明的理解一直是默认值参数在非默认值参数之后被声明为

但我只是注意到我能够键入一个违反此规则的函数声明:

  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)等。 - Michael Collier 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++ (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-20120614
  • arm-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

2 个答案:

答案 0 :(得分:1)

默认参数只允许在函数声明和lambda表达式的参数列表中使用(自C ++ 14开始),并且在函数指针声明,函数引用或 typedef声明<中不允许使用/ strong>(例如,见http://en.cppreference.com/w/cpp/language/default_arguments)。

g ++在声明接受默认函数参数的函数指针类型/非指针函数时遇到问题(请参阅bug 28262g++ v4.7.3g++ v4.8.1)。

该错误已经fixed in version 4.9.0

答案 1 :(得分:0)

不可能这样做;不是之前而不是之后 可能是您的版本中的错误。

这样做是不合逻辑的。

是否在   - 简单的函数int f( int=0, int )
  - typedef功能或
  - 模板功能

如果首先可以使用默认参数,那么当调用该函数时,编译器会出现歧义哪个参数属于第一位。