0

I am using flatbuffers - a serialization library. In the scheme I define a struct that I want to use with std::optional. but during compilation I get a warning C4324. What I understand flatbuffers use custom padding for generated structures. Is it safe to disable the warning?

The warning message is

warning C4324: 'std::_Optional_destruct_base<_Ty,true>::<unnamed-tag>': structure was padded due to alignment specifier

Visual Studio 2017, msvc 14.13.26128.

Jiří Lechner
  • 750
  • 6
  • 19
  • [C4324](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4324?view=vs-2019) – François Andrieux Mar 23 '20 at 13:33
  • C4324 is a level 4 warning. It's not a good idea to run Visual Studio at the highest warning level. Level 4 warnings are "informational warnings" that don't intend to signal a problem, bur rather to communicate extra information. It isn't practical to try to achieve 0 warnings compilation with warning level 4 in VS. – François Andrieux Mar 23 '20 at 13:35
  • I have found the warning but I don't understand its consequences. So it is only informational and the can be disabled... – Jiří Lechner Mar 23 '20 at 13:41
  • The consequences is that there are padding bytes added by the compiler. Typically, this is fine. But if the structure needs to be unpadded, say because it is conforming to an exact memory layout that requires no padding, the warning can be informative. – Eljay Mar 23 '20 at 13:45
  • 1
    @FrançoisAndrieux that is bad advice.. most code bases I work on compile cleanly on `/W4`, and a lot of the things it warns about involve possible portability issues or undefined behavior. Ignoring these makes for brittle code that is going to misbehave at some point in the future on a new platform or compiler. – Aardappel Mar 23 '20 at 15:59
  • In my experience, /W4 never produces a helpful warning. Every time I've tried it, the warnings were all for code that was completely fine and of no risk. You end up having to silence them all or making harmful changes to have them go away. /W3 with disabled language extensions is fine for writing portable code. – François Andrieux Mar 23 '20 at 16:19
  • I can't change the level for warnings - project policy. Neither I can change the build pipeline that considers any warning as error. Hence I have to resolve all warning. – Jiří Lechner Mar 24 '20 at 09:11
  • @FrançoisAndrieux My recent experience upgrading a 150k line solution from level3 to level4 warnings exposed numerous bugs. Level4 warnings expose unused variables, variables hidden by nested declaration, and unreachable code, and these three categories alone make it worth enabling Level4, as they are all patterns that can hide or embody developer errors. It's occasionally necessary to suppress an unreferenced variable warning, but it's worth it. – John Doggett Aug 16 '20 at 06:16
  • @JohnDoggert Yes, since I wrote that I've realized I was confused and thinking of `Wall` which is the same as `W4` but also turns on warnings that are off by default, like warning about padding between `class` members. Note that unses variable warnings can be fixed by casting to void : `(void)foo;` won't warn about an unused variable and has no side effects. – François Andrieux Aug 16 '20 at 12:20

1 Answers1

0

FlatBuffers generated C++ structs have explicit alignment and padding to ensure that they match the serialized data format, rather than allowing the compiler to pick these.

What I am guessing that happens here is that a std::optional is essentially a { T t; bool is_present; }, and because T requires specific alignment, the parent structure now also requires it, and the bool needs padding after it.

So that is probably benign and can probably be silenced. I'd recommend doing that for that warning only, turning it off only for the code that uses FlatBuffers and std::optional, though.

Aardappel
  • 5,559
  • 1
  • 19
  • 22