Nice. I’d not thought of that one.
For the array type it can be useful to allow implicit copy to different arithmetic types (design choice, I’m now back to explicit constructors to disallow this for what it’s worth). If allowed though, I still wanted a compile time check like this to ensure that it wasn’t happening by accident in particular circumstances.
Yes, that’s right, generic context, and you may be right on return value optimization. It was for implementing a collection of numerical functions that take array arguments, where the elements of those arrays could be of various arithmetic types, and the return type should be an array of a particular arithmetic type given promotion etc. The implementation was generic, and I was wanting to validate its correctness wrt return values having the correct arithmetic type without implicit copy.
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;
Nice, good luck with it from here!
This would be better style in my opinion, but by way of correctness it seems the more fundamental issue is “return” missing in the if… else if… blocks.
Your get() function will always just return the value of the root node. I think you mean to have return get(value, …) in each of its if statements.
Yes, std::remove_cvref_t combines the other two, in fact I believe it does so precisely (see the “Possible Implementation” on cppreference.com). The “…with a little extra” that I mention for std::decay_t in the article is that it does the same as std::remove_cvref_t plus some standardization of array and function types to pointer types (again, see the “Possible implementation” of it on cppreference.com). For my purposes it doesn’t really matter which to use, and I mostly prefer std::decay_t for its brevity.