boostorg / winapi

Windows API declarations without <windows.h>, for internal Boost use.
62 stars 58 forks source link

warning: ISO C++ prohibits anonymous structs [-Wpedantic] #62

Closed viboes closed 6 years ago

viboes commented 6 years ago
..\..\../boost/detail/winapi/system.hpp:54:9: warning: ISO C++ prohibits anonymous structs [-Wpedantic]
         } DUMMYSTRUCTNAME;
         ^
Lastique commented 6 years ago

The unnamed structs are used by Windows SDK to achieve a specific binary layout and member access syntax. Naming the structs will change the syntax to access the members, so it will be a breaking change.

The only way I know to get rid of the warning is to silence it, but the option in the compiler message is -Wpedantic, which is a blanket option, which supposedly corresponds to a list of more fine grained options. Do you know what specific option controls this warning?

viboes commented 6 years ago

Unfortunately not. Otherwise I would disable it independently.

If we can not do anything, please be free to close it as wontfix.

Kojoley commented 6 years ago

There is no specific switch in GCC, you should push/ignore/pop -Wpedantic Clang -Wgnu-anonymous-struct MSVC (before VS2017) C4094 https://msdn.microsoft.com/en-us/library/7y0f9tby.aspx

P.S: I do not know if it needs to be wrapped with __cplusplus < 201103L check

pdimov commented 6 years ago

Naming the structs will change the syntax to access the members, so it will be a breaking change.

What is the syntax that is used to access these members today? The following fails for me:

#include <boost/winapi/system.hpp>

int main()
{
    boost::winapi::SYSTEM_INFO_ si = {};
    si.wProcessorArchitecture;
}
Lastique commented 6 years ago

According to MSDN it looks like this is the intended syntax. It may be MSVC-specific though, I don't know.

pdimov commented 6 years ago

What does MSDN have to do with it? I'm asking what is the syntax that is currently supported by Winapi and that will supposedly be broken by naming the struct. The above fails on MSVC, so it's not that.

Lastique commented 6 years ago

The intended syntax is the same as for SYSTEM_INFO from Windows SDK. MSDN article shows that wProcessorArchitecture should be directly accessible as if a member of SYSTEM_INFO. It does work like that on my end with the real SYSTEM_INFO but not with the one from Boost.WinAPI. This is a bug, which I was not aware of before.

Lastique commented 6 years ago

Strangely enough, the structure is declared the same way in Windows SDK 10 and it works there somehow.

pdimov commented 6 years ago

DUMMYSTRUCTNAME and DUMMYUNIONNAME in the <windows.h> definition are macros that expand to nothing (when nameless structs are supported), which is why the intended syntax works. (The macros expand to s and u otherwise.)

Winapi however uses DUMMYSTRUCTNAME literally, it's not a macro. So the syntax to access the struct member would be

#include <boost/winapi/system.hpp>

int main()
{
    boost::winapi::SYSTEM_INFO_ si = {};
    si.DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture;
}

which I doubt anyone ever used. :-)

You are however correct, adding a name to the struct itself (rather than the member) does break the syntax, so we can't fix the warning by changing the definition of SYSTEM_INFO_.

Lastique commented 6 years ago

Ah, those lovely macros in Windows SDK.

Should be fixed in https://github.com/boostorg/winapi/commit/f004b4753d6fee6f68c90cfc65b075fa0a33d092. Hopefully, this should also silence the warnings.

Thanks for alerting me about the syntax bug.

pdimov commented 6 years ago

Confirmed warning-free on mingw g++ 7 now. Thanks.