C++ trick I pulled today. Like an explicit constructor but context dependent. Any alternatives from folks who've needed to do similar? One thing I still need to dig into a little deeper is how copy elision behaves here.
C++ trick I pulled today. Like an explicit constructor but context dependent. Any alternatives from folks who've needed to do similar? One thing I still need to dig into a little deeper is how copy elision behaves here.
It's an interesting little gotcha, but I wonder if it wouldn't be preferable to just implement a type trait like
is_expensive_conversion
that isfalse
by default andtrue
for specific cases, and juststatic_assert
this stuff.Relying on obscure implicit type conversion mechanics for this feature feels like being too clever for your own good.
That's a fair criticism around relying on implicit type conversion mechanics, and part of the tradeoff to make. On the other hand, I imagine (and my imagination may be limited) that one downside of
static_assert
is to increase verbosity, something like:auto r = f(); static_assert(std::is_same_v<decltype(r),MyReturnType>> || !is_expensive_conversion_v<MyReturnType>); return r;