1

I have the following code:

struct S {
    const bool x = true;
    constexpr const bool& f() { return x; }
} s{};

int main() {
  static_assert(s.f()); // error: s is not declared as 'constexpr';
}

Note that, this is a dupe for the question How to make this pointer constant expression because the the expression E = s.f() does not evaluates lvalue-to-rvalue conversion ([expr.const]/(5.8)).

Also, this question is not a dupe for the question How to get std array size in a constant expression because the expression E = s.f() does evaluates this in a constexpr function S::f that's evaluated as part of the expression s.f() ([expr.const]/(5.1)).

Now my question is, which bullet in [expr.const]/5 (if any) is applied to the above example?

mada
  • 1,646
  • 1
  • 15
  • _does not evaluates lvalue-to-rvalue conversion_ ??? `static_assert` is interested in the value of the expression. How do you get one without L2R conversion? – Language Lawyer Sep 13 '22 at 09:10
  • @LanguageLawyer Are you looking at the questions mentioned above? – mada Sep 13 '22 at 09:12
  • @lorro What will be changed if you do so? Really nothing. – mada Sep 13 '22 at 09:15
  • 1
    In `static_assert`, L2R conversion is performed on `s.f()`. `s.f()` denotes `s.x`. What is the difference between performing L2R conversion on `x` in `return x` and here? The expressions denote the same object. – Language Lawyer Sep 13 '22 at 09:16
  • @LanguageLawyer - I disagree. Binding a reference of type `const bool&` to an lvalue of type `const bool` doesn't involve lvalue-to-rvalue conversion. – mada Sep 13 '22 at 09:18
  • 2
    But the `static_assert` itself does involve it. It asserts on the *value* of `s.f()`. That makes `s.f()` not a constant expression. – StoryTeller - Unslander Monica Sep 13 '22 at 09:21
  • Because `s` is not `constexpr`, that makes `this` not `constexpr`, that means `s.f()` is not a `constexpr` function in that context. – Eljay Sep 13 '22 at 14:28

0 Answers0