cnl icon indicating copy to clipboard operation
cnl copied to clipboard

Try to be more `explicit`

Open johnmcfarlane opened this issue 7 years ago • 8 comments

Consider:

static_assert(identical(
        elastic_fixed_point<2, 2>{1.5} << 1,
        elastic_fixed_point<2, 2>{3}));

Looks OK right? But increase the shiftage:

static_assert(identical(
        elastic_fixed_point<2, 2>{1.5} << 123,
        elastic_fixed_point<2, 2>{3}));

Oh no, it still compiles! Trick! The exponent is 2 so both sides of the equality are zero.

int would never allow you to do that:

static_assert(int(0.5f) == int(.25f), "");
static_assert(int{0.5f} == int{.25f}, "");

This doesn't compile because float to int narrows and curlies don't allow that. We cannot eliminate all gotchas like the elastic_fixed_point one above. But we may avoid lulling the user into a false sense of security by matching what fundamental types do a little more closely.

johnmcfarlane avatar Jan 13 '18 23:01 johnmcfarlane

Another good example of where expectation is confounded is:

auto n = elastic_integer<5>{elastic_integer<10>{1000}};

johnmcfarlane avatar Jun 17 '18 09:06 johnmcfarlane

C++20 helps: http://en.cppreference.com/w/cpp/language/explicit

johnmcfarlane avatar Jun 20 '18 04:06 johnmcfarlane

For previous revisions use techniques described in P0892.

johnmcfarlane avatar Aug 12 '18 18:08 johnmcfarlane

Hi John! I'll will try correct this issue and issues quoted here. You have a check list and or any advice to give me? I'll target c++20.

Thanks in Avance, Dani. :-)

danieagle avatar Aug 12 '20 18:08 danieagle

Hi @danieagle this might not be the most straight-forward issue to fix right away. Library needs upgrading to C++20 before the explicit(expression) form can be used. There might be easier issues to tackle. And did you mention you had some repros that could be turned into issues. Perhaps they could be branches with new tests that fail -- TDD-style.

johnmcfarlane avatar Aug 13 '20 16:08 johnmcfarlane

Hi! Ok! I'll do it. :-) Really(!) Thanks!

[]'s Dani. :-)

danieagle avatar Aug 16 '20 06:08 danieagle

Possibly related is the ambiguity reported by compiler here.

johnmcfarlane avatar Feb 24 '21 14:02 johnmcfarlane

May result in removal of hicpp-explicit-conversions and google-explicit-constructor deviations.

johnmcfarlane avatar Mar 28 '21 10:03 johnmcfarlane