Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.const] p10 converted constant expression of type bool vs contextually converted constant expression of type bool #4855

Closed
xmh0511 opened this issue Sep 1, 2021 · 1 comment

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Sep 1, 2021

struct C{
    constexpr operator bool(){
        return true;
    }
};
template<bool B>
struct D{};

static_assert(C{});   // #1 allow
static_assert(sizeof(double)); // #2 allow
D<sizeof(double)> d;  // #3 disallow

[expr.const] p10 says

A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only.

  • [...]
  • integral conversions other than narrowing conversions,
  • [...]

and where the reference binding (if any) binds directly.

A contextually converted constant expression of type bool is an expression, contextually converted to bool ([conv]), where the converted expression is a constant expression and the conversion sequence contains only the conversions above.

I don't know which conversion does the emphasized word refer to. Does it refer to the conversions listed in above? Obviously, the value of sizeof(double) converted to bool is narrowing conversion, which violates integral conversions other than narrowing conversions, however, it does allow. If we say "the conversions above" refers to Boolean conversions, but user-defined conversion is also allowed at #1. It seems "the conversions above" is vague here. contextually converted constant expression of type bool is introduced by CWG2039, it claims that

This allows the expression to have any type that can be converted to bool, which is too lenient; it should instead say something like “a converted constant expression of type bool (7.7 [expr.const]).” This would include the conversion to bool in the determination of whether the expression is constant or not and would also disallow narrowing conversions.

However, the behavior that "contextually converted constant expression of type bool" permits is different from that for "converted constant expression of type bool", #3 proves that.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Sep 1, 2021

That paper(cwg2039) is outdated, "contextually converted to bool" is different from "contextually converted constant expression of type bool".

@xmh0511 xmh0511 closed this as completed Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant