更新到 GCC 12 后的错误

Errors after updating to GCC 12

我有一个项目,我正在使用 datetimepp 库,它一直运行良好。但是我最近做了 pacman -Syu 并更新了 gcc。然后我编译了项目(之前已经正确编译)并编译了它。我有多个错误抱怨“默认参数重新定义”

datetimepp/datetime.h:311:96: error: redefinition of default argument for ‘typename std::enable_if<std::is_floating_point<_Tp>::value>::type* <anonymous>’
  311 | template<class Scalar, typename std::enable_if<std::is_floating_point<Scalar>::value>::type* = nullptr>
      |                                                                                                ^~~~~~~
datetimepp/datetime.h:73:96: note: original definition appeared here
   73 | template<class Scalar, typename std::enable_if<std::is_floating_point<Scalar>::value>::type* = nullptr>
      |                                                                                                ^~~~~~~
datetimepp/datetime.h:323:90: error: redefinition of default argument for ‘typename std::enable_if<std::is_integral<_Tp>::value>::type* <anonymous>’
  323 | template<class Scalar, typename std::enable_if<std::is_integral<Scalar>::value>::type* = nullptr>
      |                                                                                          ^~~~~~~
datetimepp/datetime.h:76:90: note: original definition appeared here
   76 | template<class Scalar, typename std::enable_if<std::is_integral<Scalar>::value>::type* = nullptr>
      |                                                                                          ^~~~~~~
datetimepp/datetime.h:335:96: error: redefinition of default argument for ‘typename std::enable_if<std::is_floating_point<_Tp>::value>::type* <anonymous>’
  335 | template<class Scalar, typename std::enable_if<std::is_floating_point<Scalar>::value>::type* = nullptr>
      |                                                                                                ^~~~~~~
datetimepp/datetime.h:79:96: note: original definition appeared here
   79 | template<class Scalar, typename std::enable_if<std::is_floating_point<Scalar>::value>::type* = nullptr>
      |                                                                                                ^~~~~~~
datetimepp/datetime.h:342:90: error: redefinition of default argument for ‘typename std::enable_if<std::is_integral<_Tp>::value>::type* <anonymous>’
  342 | template<class Scalar, typename std::enable_if<std::is_integral<Scalar>::value>::type* = nullptr>
      |                                                                                          ^~~~~~~
datetimepp/datetime.h:82:90: note: original definition appeared here
   82 | template<class Scalar, typename std::enable_if<std::is_integral<Scalar>::value>::type* = nullptr>

为了确认程序在我更新之前确实可以运行,我回滚了 gcc 更新并且程序编译成功。

这是我应该修复的程序问题,还是 g++ 问题?

默认参数只能在声明或定义中指定,但在您的情况下,您已在两个地方指定它们。

void foo(int x = 10);
void foo(int x = 10){} // error. redefinition of default arg
void foo(int x){}
foo(); // ok. default arg is 10

所以您应该从声明或定义中删除它们,最好是定义,因为在定义和声明之间,您将无法使用这些默认参数。

void foo(int x);
foo(); // error. no default arg
void foo(int x = 10){}
foo(); // ok