Thanks and kind regards,
dyp
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.
#include <string>
struct R { constexpr R(std::string& a) : r(a) {} std::string& r; };
constexpr bool is_same(R lhs, R rhs) { return &lhs.r == &rhs.r; }
int main() {
std::string s0, s1;
constexpr bool fun_test = is_same(s0, s1);
}
To clarify, this is legal:#include <string>
struct R { constexpr R(std::string& a) : r(a) {} std::string& r; };
constexpr bool is_same(R lhs, R rhs) { return &lhs.r == &rhs.r; }
int main() {
std::string s0, s1;
constexpr bool fun_test = is_same(s0, s1);
}And the fix would be to add a bullet to [expr.const]/2.9 to make replacing R by std::string& also legal?
Thank you very much for your answers and clarifications.
I might be nit-picking, but is the lifetime of a reference well enough
defined to be used in this wording? For objects, there are the extensive
[basic.life] rules, but for references, there's only the IMHO vague
[basic.stc]p3, "The lifetime of a reference is its storage duration."
There's a related defect: http://wg21.cmeerw.net/cwg/issue2012
Kind regards,
dyp
On 12.08.2015 20:03, Richard Smith wrote:
> On Wed, Aug 12, 2015 at 3:41 AM, Edward Catmur <e...@catmur.co.uk> wrote:
>
>> To clarify, this is legal:
>>
>> #include <string>
>> struct R { constexpr R(std::string& a) : r(a) {} std::string& r; };
>> constexpr bool is_same(R lhs, R rhs) { return &lhs.r == &rhs.r; }
>> int main() {
>> std::string s0, s1;
>> constexpr bool fun_test = is_same(s0, s1);
>> }
>>
>> And the fix would be to add a bullet to *[expr.const]*/2.9 to make
>> replacing R by std::string& also legal?
>>
>
> I asked to get a core issue opened for this; here's the wording change I
> suggested:
>
> Change in 5.20 [expr.const] bullet 2.9 and remove the sub-bullets:
>
> "an id-expression that refers to a variable or data member of reference
> type unless the reference has a preceding initialization and either
> <del> -- </del> it is initialized with a constant expression or
> <del> -- it is a non-static data member of an object
> whose<del><ins>its</ins> lifetime began within the evaluation of e;"
>
This is a bug in the constant evaluation rules. It should be permissible to use an id-expression that refers to a variable of reference type if its lifetime began within the evaluation.
The "preceding initialization" part isn't the problem. The problem is that neither of the current bullets is satisfied: the references lhs and rhs are not initialized by constant expressions, and are not non-static data members, so they cannot be used within a constant expression. With the proposed change, they can be used, because their lifetimes began within the evaluation of the initializer of fun_test.
He refers to the references' lifetime. So does dyp's suggested wording (its last part).
And I agree with him, which IMHO means, issue 2012 needs to be approved and reflected in the Standard, so that the term "lifetime", as applied to a reference, is well defined.