gracicot / kangaru

🦘 A dependency injection container for C++11, C++14 and later
MIT License
478 stars 40 forks source link

Deprecated warning in C++23 due to use of std::aligned_storage #116

Closed WopsS closed 1 year ago

WopsS commented 1 year ago

Describe the bug When a project utilizes this library and is compiled with C++23, a deprecation warning is issued by the compiler. This warning is associated with the use of std::aligned_storage<Len, Align>, which is deprecated in C++23. The deprecation warning message is as follows:

warning C4996: 'std::aligned_storage<8,8>': warning STL4034: std::aligned_storage and std::aligned_storage_t are deprecated in C++23. Prefer alignas(T) std::byte t_buff[sizeof(T)]. You can define _SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING or _SILENCE_ALL_CXX23_DEPRECATION_WARNINGS to suppress this warning.

The warning is triggered by the following line of code: https://github.com/gracicot/kangaru/blob/36f70dba250f070c7abf844af1c0aefb55640907/include/kangaru/detail/traits.hpp#L58

To Reproduce

  1. Compile a project that uses this library with C++23.

Expected behavior

The expected behavior is for the project to compile without any deprecation warnings, without the need to suppress them.

Desktop (please complete the following information):

Additional context

A potential solution could be to create a struct using alignas. However, if a project enables the /W4 flag, it will yield Compiler Warning (level 4) C4324. An alternative workaround may involve using #pragma warning(suppress : 4324), but this seems like a less-than-ideal, somewhat hacky solution.

Is alignment necessary for this library? If not, the code could be refactored to eliminate the need for alignment.

gracicot commented 1 year ago

Thank you for the report! I'll patch it as soon as I find some time

gracicot commented 1 year ago

Fixed in 686d36af8a614db5e74fc88bcc21e3f72e80e862 on master, thank you!

WopsS commented 1 year ago

Thanks! The issue is that right now it doesn't compile :(

Maybe just char[size] would be enough here (https://github.com/gracicot/kangaru/commit/686d36af8a614db5e74fc88bcc21e3f72e80e862). If alignas is really necessary than something like should work:

template<std::size_t size, std::size_t align>
struct aligned_storage_t
{
    alignas(align) char data[size];
};
gracicot commented 1 year ago

Yeah I saw the pipeline failing on non gcc compiler, seems like I used an extension without knowing. I'll probably just put alignas(align) unsigned char data[size]; at the places I used aligned_storage_t in the past and removing that alias.

(alignment is actually important or there will be undefined behaviour, x86 just enable broken code to work in some common case)

gracicot commented 1 year ago

It should be okay now, I can see the master branch passing on all runners

WopsS commented 1 year ago

Thank you for the fix! :D