The International Simutrans Forum

 

Author Topic: Fixes for warnings when compiling with GCC 9  (Read 346 times)

0 Members and 2 Guests are viewing this topic.

Offline ceeac

  • *
  • Posts: 54
Fixes for warnings when compiling with GCC 9
« on: October 27, 2019, 03:32:26 PM »
This patch fixes the following warnings (where possible) when compiling with GCC 9:
  • Wparentheses
  • Wimplicit-fallthrough
  • Wdeprecated-copy

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4607
  • Languages: EN, DE, AT
Re: Fixes for warnings when compiling with GCC 9
« Reply #1 on: November 02, 2019, 09:13:23 AM »
What is the reason to implement assignment operators?

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5555
  • Languages: EN, NO
Re: Fixes for warnings when compiling with GCC 9
« Reply #2 on: November 02, 2019, 09:33:10 AM »
Although I haven't tested, I assume it is because the lack of explicit definition give warnings with GCC 9. Since the classes have one or more constructors, generation of automatic assignment operators are deprecated in some version of the C++ standard, perhaps one that is default with GCC 9. Whether the assignment operators are actually used, is not something I know how to check without reading through the code.

It think it is possible to say that you actually do want the compiler to generate the default assignment operators, although that will require using a relatively new keyword. Since the copy constructor is hand-written, I think it is best to hand write the assignment operator as well.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 9584
  • Languages: De,EN,JP
Re: Fixes for warnings when compiling with GCC 9
« Reply #3 on: November 02, 2019, 02:41:23 PM »
The copy contructor is only there, because otherwise one could not convert a scr_coord into a scr_size.

Personally, I think that having the copy constructor doing something else than the assignment operator is a really bad idea that should rather generate lots of warnings. I.e. defining both should generate warnings ...

As such, I would rather not define the assignment operator. I seems rather dangerous to me.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5555
  • Languages: EN, NO
Re: Fixes for warnings when compiling with GCC 9
« Reply #4 on: November 03, 2019, 09:16:24 AM »
Personally, I think that having the copy constructor doing something else than the assignment operator is a really bad idea that should rather generate lots of warnings. I.e. defining both should generate warnings ...
The fact that the standard is on its way to disabling automatic generation of copy assignment operators when copy constructors are defined, likely means that experience has shown that whenever custom copy constructors are around, the default copy assignment operator ends up doing something wrong. The programmer may mess up when having to define both manually, but then at least the language has forced the programmer to think through what copying such an object means, rather than lulling the programmer into thinking it will just work.

There is also the rule of three, which includes destructors as well.

The copy contructor is only there, because otherwise one could not convert a scr_coord into a scr_size.
Copy constructors do not convert types. C++ has a special class of functions that can be defined for converting between types. Unfortunately, is seems these have to be declared on the from type, which is some cases may cause dependencies that are backwards compared to what may feel natural.

Offline ceeac

  • *
  • Posts: 54
Re: Fixes for warnings when compiling with GCC 9
« Reply #5 on: November 09, 2019, 11:01:01 PM »
Update: Instead of defining assignment operators, I opted to remove the user-defined copy constructors since the compiler-generated ones already do the correct thing.

Although I haven't tested, I assume it is because the lack of explicit definition give warnings with GCC 9. Since the classes have one or more constructors, generation of automatic assignment operators are deprecated in some version of the C++ standard, perhaps one that is default with GCC 9. Whether the assignment operators are actually used, is not something I know how to check without reading through the code.
The behaviour is deprecated since C++11, however only GCC 9 warns about it by default. This is not specifc to the default C++ version GCC uses since GCC enables C++14 with GNU extensions by default since version 5 (I think) and also because Simutrans enables a pre-C++11 standard explicitly anyway.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5555
  • Languages: EN, NO
Re: Fixes for warnings when compiling with GCC 9
« Reply #6 on: November 10, 2019, 09:35:47 AM »
Simutrans enables a pre-C++11 standard explicitly anyway
I can't see that it does. And if Simutrans is asking for pre-C++11, GCC should not give these warnings (according for the manual).

Otherwise, the patch looks fine. Unless there are same misbehaving compilers that don't generate default copy constructor if there are any custom constructors.