asio icon indicating copy to clipboard operation
asio copied to clipboard

ASIO_NODISCARD uses __cplusplus, which is broken-by-default on MSVC

Open MHebes opened this issue 3 years ago • 0 comments

The __cplusplus macro is broken by default on MSVC and can be fixed by a compiler switch (/Zc:__cplusplus).

Without the compiler flag, ASIO_NODISCARD is defined to a blank macro because of a __cplusplus check:

#if !defined(ASIO_NODISCARD)
# if defined(__has_cpp_attribute)
#  if __has_cpp_attribute(nodiscard)
#   if (__cplusplus >= 201703)  // always false by default on modern MSVC
#    define ASIO_NODISCARD [[nodiscard]]
#   endif // (__cplusplus >= 201703)
#  endif // __has_cpp_attribute(nodiscard)
# endif // defined(__has_cpp_attribute)
#endif // !defined(ASIO_NODISCARD)
#if !defined(ASIO_NODISCARD)
# define ASIO_NODISCARD
#endif // !defined(ASIO_NODISCARD)

That's pretty fiddly for users, and most other config.hpp macros seem to use _MSC_VER instead when ASIO_MSVC is defined anyways, so maybe it would be good to switch to something like:

#if !defined(ASIO_NODISCARD)
# if defined(ASIO_MSVC)
#  if (_MSC_VER >= 1928) && (_MSVC_LANG >= 201705)
#   define ASIO_NODISCARD [[nodiscard]]
#  endif
# else
#  if defined(__has_cpp_attribute)
#   if __has_cpp_attribute(nodiscard)
#    if (__cplusplus >= 201703)
#     define ASIO_NODISCARD [[nodiscard]]
#    endif // (__cplusplus >= 201703)
#   endif // __has_cpp_attribute(nodiscard)
#  endif // defined(__has_cpp_attribute)
# endif
#endif // !defined(ASIO_NODISCARD)
#if !defined(ASIO_NODISCARD)
# define ASIO_NODISCARD
#endif // !defined(ASIO_NODISCARD)

(Not sure what the right _MSC_VER and _MSVC_LANG values are, I stole them from ASIO_HAS_STD_COROUTINE)

MHebes avatar Aug 11 '22 05:08 MHebes