Skip to content

Commit 0d8f3cc

Browse files
committed
slot, signal: Avoid compiler warnings from function pointer conversions
gcc8 -Wextra prints a warning when reinterpret_cast is used for conversion between different types of function pointers. Avoid that by adding sigc::internal::bitwise_equivalent_cast<>() with a union with members of the two types of function pointers. Fixes libsigcplusplus#1
1 parent efe4089 commit 0d8f3cc

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

sigc++/functors/macros/slot.h.m4

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ FOR(1, $1,[
6363
inline T_return operator()(LOOP(arg%1_type_ _A_a%1, $1)) const
6464
{
6565
if (!empty() && !blocked())
66-
return (reinterpret_cast<call_type>(slot_base::rep_->call_))(LIST(slot_base::rep_, LOOP(_A_a%1, $1)));
66+
return (internal::bitwise_equivalent_cast<call_type>(slot_base::rep_->call_))(LIST(slot_base::rep_, LOOP(_A_a%1, $1)));
6767
return T_return();
6868
}
6969
@@ -355,7 +355,7 @@ ifelse($1,0,[
355355
* @return A function pointer formed from call_it().
356356
*/
357357
static hook address()
358-
{ return reinterpret_cast<hook>(&call_it); }
358+
{ return bitwise_equivalent_cast<hook>(&call_it); }
359359
};
360360
361361
])
@@ -378,6 +378,25 @@ namespace sigc {
378378

379379
namespace internal {
380380

381+
// Conversion between different types of function pointers with
382+
// reinterpret_cast can make gcc8 print a warning.
383+
// https://github.com/libsigcplusplus/libsigcplusplus/issues/1
384+
/** Returns the supplied bit pattern, interpreted as another type.
385+
*
386+
* When reinterpret_cast causes a compiler warning or error, this function
387+
* may work. Intended mainly for conversion between different types of pointers.
388+
*/
389+
template <typename out_type, typename in_type>
390+
inline out_type bitwise_equivalent_cast(in_type in)
391+
{
392+
union {
393+
in_type in;
394+
out_type out;
395+
} u;
396+
u.in = in;
397+
return u.out;
398+
}
399+
381400
/** A typed slot_rep.
382401
* A typed slot_rep holds a functor that can be invoked from
383402
* slot::operator()(). visit_each() is used to visit the functor's
@@ -483,7 +502,7 @@ struct slot_call
483502
* @return A function pointer formed from call_it().
484503
*/
485504
static hook address()
486-
{ return reinterpret_cast<hook>(&call_it); }
505+
{ return bitwise_equivalent_cast<hook>(&call_it); }
487506
};
488507

489508
/** Abstracts functor execution.
@@ -515,7 +534,7 @@ struct slot_call<T_functor, T_return>
515534
* @return A function pointer formed from call_it().
516535
*/
517536
static hook address()
518-
{ return reinterpret_cast<hook>(&call_it); }
537+
{ return bitwise_equivalent_cast<hook>(&call_it); }
519538
};
520539

521540
} /* namespace internal */
@@ -575,7 +594,7 @@ public:
575594
inline T_return operator()(type_trait_take_t<T_arg>... _A_a) const
576595
{
577596
if (!empty() && !blocked())
578-
return (reinterpret_cast<call_type>(slot_base::rep_->call_))(slot_base::rep_, _A_a...);
597+
return (internal::bitwise_equivalent_cast<call_type>(slot_base::rep_->call_))(slot_base::rep_, _A_a...);
579598
return T_return();
580599
}
581600

sigc++/macros/signal.h.m4

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ifelse($1,0,[dnl
5151
* @return The slot's return value.
5252
*/
5353
T_return operator()(const slot_type& _A_slot) const
54-
{ return (reinterpret_cast<typename slot_type::call_type>(_A_slot.rep_->call_))(LIST(_A_slot.rep_, LOOP(_A_a%1_, $1))); }
54+
{ return (bitwise_equivalent_cast<typename slot_type::call_type>(_A_slot.rep_->call_))(LIST(_A_slot.rep_, LOOP(_A_a%1_, $1))); }
5555
dnl T_return operator()(const slot_type& _A_slot) const
5656
dnl { return _A_slot(LOOP(_A_a%1_, $1)); }
5757
@@ -150,12 +150,12 @@ FOR(1, $1,[
150150
if (it == slots.end())
151151
return T_return(); // note that 'T_return r_();' doesn't work => define 'r_' after this line and initialize as follows:
152152
153-
r_ = (reinterpret_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
153+
r_ = (bitwise_equivalent_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
154154
for (++it; it != slots.end(); ++it)
155155
{
156156
if (it->empty() || it->blocked())
157157
continue;
158-
r_ = (reinterpret_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
158+
r_ = (bitwise_equivalent_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
159159
}
160160
}
161161
@@ -201,12 +201,12 @@ FOR(1, $1,[
201201
if (it == reverse_iterator_type(slots.begin()))
202202
return T_return(); // note that 'T_return r_();' doesn't work => define 'r_' after this line and initialize as follows:
203203
204-
r_ = (reinterpret_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
204+
r_ = (bitwise_equivalent_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
205205
for (++it; it != reverse_iterator_type(slots.begin()); ++it)
206206
{
207207
if (it->empty() || it->blocked())
208208
continue;
209-
r_ = (reinterpret_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
209+
r_ = (bitwise_equivalent_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
210210
}
211211
}
212212
@@ -247,7 +247,7 @@ FOR(1, $1,[
247247
{
248248
if (slot.empty() || slot.blocked())
249249
continue;
250-
(reinterpret_cast<call_type>(slot.rep_->call_))(LIST(slot.rep_, LOOP(_A_a%1, $1)));
250+
(bitwise_equivalent_cast<call_type>(slot.rep_->call_))(LIST(slot.rep_, LOOP(_A_a%1, $1)));
251251
}
252252
}
253253
@@ -278,7 +278,7 @@ FOR(1, $1,[
278278
{
279279
if (it->empty() || it->blocked())
280280
continue;
281-
(reinterpret_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
281+
(bitwise_equivalent_cast<call_type>(it->rep_->call_))(LIST(it->rep_, LOOP(_A_a%1, $1)));
282282
}
283283
}
284284
_DEPRECATE_IFDEF_END

0 commit comments

Comments
 (0)