Skip to content

Commit 04b4a67

Browse files
committed
Introducing macro: PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES
1 parent dcb5139 commit 04b4a67

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

include/pybind11/attr.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -518,26 +518,26 @@ template <typename... Args> struct process_attributes {
518518
static void init(const Args&... args, function_record *r) {
519519
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
520520
PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r);
521-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
522-
silence_unused_warnings(unused);
521+
PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(
522+
{0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...});
523523
}
524524
static void init(const Args&... args, type_record *r) {
525525
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
526526
PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r);
527-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
528-
silence_unused_warnings(unused);
527+
PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(
528+
{0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...});
529529
}
530530
static void precall(function_call &call) {
531531
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call);
532-
// int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... };
533-
// silence_unused_warnings(unused);
534-
(int []) { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... };
532+
PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(
533+
{0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0)...});
535534
}
536535
static void postcall(function_call &call, handle fn_ret) {
537536
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret);
538537
PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret);
539-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0) ... };
540-
silence_unused_warnings(unused);
538+
PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(
539+
{0,
540+
(process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0)...});
541541
}
542542
};
543543

include/pybind11/cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,8 +1237,8 @@ class unpacking_collector {
12371237
// Tuples aren't (easily) resizable so a list is needed for collection,
12381238
// but the actual function call strictly requires a tuple.
12391239
auto args_list = list();
1240-
int unused[] = { 0, (process(args_list, std::forward<Ts>(values)), 0)... };
1241-
silence_unused_warnings(unused);
1240+
PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(
1241+
{0, (process(args_list, std::forward<Ts>(values)), 0)...});
12421242

12431243
m_args = std::move(args_list);
12441244
}

include/pybind11/detail/common.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,17 @@ inline void silence_unused_warnings(Args &&...) {}
955955
# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...)
956956
#endif
957957

958+
// MSVC error C4576: a parenthesized type followed by an initializer list is a
959+
// non-standard explicit type conversion syntax
960+
// clang: expression result unused [-Wunused-value]
961+
#if defined(_MSC_VER) || defined(__clang__) // All versions (as of July 2021).
962+
# define PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(...) \
963+
int dummy[] = __VA_ARGS__; \
964+
detail::silence_unused_warnings(dummy)
965+
#else
966+
# define PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(...) (int[]) __VA_ARGS__
967+
#endif
968+
958969
#if defined(_MSC_VER) // All versions (as of July 2021).
959970

960971
// warning C4127: Conditional expression is constant

0 commit comments

Comments
 (0)