I wrote a class to facilitate erasing styles, which has the following constructors:
class Envelope {
public:
Envelope() {}
template<typename Runnable>
Envelope(Runnable runnable)
: m_runFunc(&Envelope::RunAndDeleteRunnable<Runnable>), m_runnable(new Runnable(runnable)) {
}
template<typename Runnable>
Envelope(Runnable * runnable)
: m_runFunc(&Envelope::RunRunnable<Runnable>), m_runnable(runnable) {
}
};
I want to rewrite the first constructor, different from the standard one, instead of the link ( Runnable & runnable, not Runnable runnable), but if I do this, then copying using the non-constant Envelope/ p>
Envelope next(...);
Envelope otherNext(next);
calls this constructor, not the copy constructor, and I get a stack overflow.
I think I can prevent this constructor from being called when Runnable== Envelopewith std::enable_ifso
template<typename Runnable = typename std::enable_if<std::negate<std::is_same<Runnable, Nova::Envelope>>::value, Runnable>::type>
Envelope(Runnable & runnable)
: m_runFunc(&Envelope::RunAndDeleteRunnable<Runnable>), m_runnable(new Runnable(runnable)) {
}
and it compiles fine (although it causes some intellisense errors in Visual Studio 2015, which is slightly annoying), but it does not prevent the constructor from being called with a non-constant Envelopeand causing a stack overflow.
, .