2

In the following code:

int main(){
  constexpr int a = 123;
  []() {
    int v = (a,123);
    };
}

Obviously, this code doesn't require a to be potentially-evaluated for it's constexpr, so it's not ODR-used. Thus it should't require capture of any form.

However, only GCC succeeded to compile this snippet.

Is this a bug existing both in MSVC and clang? Or did I miss something? Whose fault is it?

con ko
  • 1,368
  • 5
  • 18
  • the downvoter may give me a reason? – con ko Mar 31 '20 at 13:39
  • [..or may not.](https://meta.stackoverflow.com/a/285777/4117728) – 463035818_is_not_an_ai Mar 31 '20 at 13:42
  • (not the down voter) `a` is a [*discarded-value expression*](https://timsong-cpp.github.io/cppwp/expr.prop#def:discarded-value_expression). AFAIK, that requires the object to be defined, but I'm not sure enough for an answer. – NathanOliver Mar 31 '20 at 13:42
  • @idclev463035818 I understand that... I'm just simply requesting him or her to give me a reason to improve my question, or I will be very puzzled. I'm not blaming. – con ko Mar 31 '20 at 13:45
  • 3
    Actually, looks like I'm wrong. related/dupe: https://stackoverflow.com/questions/33873788/how-can-i-use-a-constexpr-value-in-a-lambda – NathanOliver Mar 31 '20 at 13:46
  • 1
    @JohnDing I know your situation very well and my first reflex is often the very same, though experience tells that what you want is constructive critisism and if a user decided to vote instead of leave a comment with constructive critisism then asking to explain the vote is futile. Users that want to leave a constructive comment usually do so – 463035818_is_not_an_ai Mar 31 '20 at 13:48
  • @NathanOliver Ahh, it's strange that a bug detected 4 years ago is still not fixed now. Feel free to close this qustion, because it's almost identical with that one, except that it's very hard to find that question, for the title is so confusing... – con ko Mar 31 '20 at 13:52
  • Yeah, for me to find it I used *can capture constexpr variable without default capture in lambda gcc* as my search query. I've now changed the old Q's title to *Can I use a constexpr value in a lambda without capturing it?* – NathanOliver Mar 31 '20 at 13:55
  • *" its value is discarded, so it's not ODR-used"*. It seems wrong, change to `(&a, 123)` and then gcc requires the capture. – Jarod42 Mar 31 '20 at 13:58
  • @Jarod42 They are completely different behaviors. Operator `&` requires Odr-use on its oprand. – con ko Mar 31 '20 at 14:03
  • 1
    I meant, it is not because expression is discarded, that `a` should not be captured. if you change to `int v = a;`, most compiler compiles [Demo](https://godbolt.org/z/AYv_QZ). – Jarod42 Mar 31 '20 at 15:09
  • @Jarod42 I unserstood what you mean, I'll edit my post, thanks. I've seen in cppreferece that only the second parameter is added in to the set of *potentially-evaluation*. But I'm not sure whether the *value-discarded expression* `a` is an ODR-use or not... Anyway, retrieving value from a compile-time constant is not ODR-use, of course, which I didn't notice – con ko Mar 31 '20 at 23:59

0 Answers0