c++ - 函数 typedef 声明符 - 默认参数位置

标签 c++ function parameters typedef default-parameters

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

但我刚刚注意到我可以 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 28262g++ v4.7.3g++ v4.8.1 )。

该错误已为 fixed in version 4.9.0 .

关于c++ - 函数 typedef 声明符 - 默认参数位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244798/

相关文章:

c++ - UML 类图的表示法

c++ - 使用 makefile 编译 C++ 11 程序的简单方法

python - 运行KaplanMeier模型的函数(Python)

parameters - Scikit-learn:我们如何定义网格搜索的距离度量参数

powershell - Powershell:具有转义参数(-%)的调用运算符(&)无法与非静态参数一起使用

c++ - 从字符串中删除重复的字符

c++ - 如何在带有密码身份验证的 linux 中以 root 身份使用 C 或 C++ 运行没有 pam 的命令

javascript - 等待 javascript 函数再次被调用

Java 按值传递还是按引用传递?

javascript - 用 js 函数 window.history.back() 添加一个参数