34227

List using with references, changes behavior when used as a member

Question:

Experimenting with this question/answer <a href="https://stackoverflow.com/a/50649120/225186" rel="nofollow">https://stackoverflow.com/a/50649120/225186</a> I produced what seems to be a legal recursive self referential class that implements a circular list:

struct node{ int val; node const& next; }; int main(){ node s{3, {4, s}}; assert(s.val == 3); assert(s.next.val == 4); assert(&s.next.next == &s); assert(s.next.next.val == 3); }

However, when I try put this as a member of a larger class I get a warning from the compiler and the behavior changes.

struct A{ node n; int i; A(int a, int b) : n{a, {b, n}}{} // warning here, also tried n{a, node{b, n}} }; int main(){ A a(3, 4); assert( a.n.val == 3 ); assert(&(a.n.next.next) == &(a.n)); // this assert fail and assert( a.n.next.val == 4 ); // segmentation fault here }

The warning I get is gcc: warning: a temporary bound to ‘A::n’ only persists until the constructor exits [-Wextra]. I don't believe that the warning is correct, however it is consistent with the later runtime error.

I admit that the class is non-conventional, however, <strong>how can it be that a class that it changes behavior inside a class?</strong>

<strong>Am I missing something?</strong>

Answer1:

Aggregate initialization allows binding references to temporaries (<a href="https://stackoverflow.com/questions/35313292/aggregate-reference-member-and-temporary-lifetime" rel="nofollow">and this causes lifetime extension</a>). Your first example is <a href="https://en.cppreference.com/w/cpp/language/aggregate_initialization" rel="nofollow">aggregate initialization</a> because node is an aggregate.

However, in a constructor member initializer list, it is ill-formed to bind a reference to a temporary (C++17 class.base.init/11). This is because there is no lifetime extension in that situation , and allowing it would inevitably produce a dangling reference. In the second example node is not an aggregate because it has a user-provided constructor.

Recommend

  • Pop-up window from actionbar
  • Promise is synchronous or asynchronous in node js
  • Where are node-gyp's [options] documented?
  • Javascript : Close open HTML tags in a string
  • How to show a comma separated number with StringFormat in XAML?
  • how to get current distance between iPhone and paired Apple Watch
  • How to integrate application with excel macros?
  • opencv error: assertion failed (size.width>0 && size.height>0) in unknown function lin
  • Heroku Rails 4— FileNotFound jquery.ui.all
  • Convert a hashmap to a json string in spring-boot
  • reason generics were introduced in java 5
  • Inject Javascript code into a web page
  • Efficient looping in objective-c
  • Mapping google maps coordinate from a javascript variable
  • RSRuntimeException: Loading of ScriptC script failed (build tools 19.0.2)
  • How I can specify how rainbow color scheme should be converted to grayscale
  • How to debug component/typescript code when running Protractor
  • Salesforce API: How to identify a Case from an email reference code (“[Ref: … :Ref]”)?
  • Django non-rel createsuperuser fails due to non-ascii characters
  • Tableview make specific cell or row editable
  • Firebase suddenly reports invalid signature
  • Calling a flash ExternalInterface in swiffyobject
  • VB.net Service Programming and using TCP Sockets
  • Boolean filter using a timestamp value on a dataframe in Python
  • css: column-count 3, image floating spanning 2, chrome not playing. why?
  • PHP: Need to close STDIN in order to read STDOUT?
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • AWS RDS Parameter Group not changing MySQL encoding
  • .Net core Hosted Services guaranteed to complete
  • Controller or RestController
  • Year over Year Stats from a Crossfilter Dataset
  • Multiplying polynomials/simplifying like terms
  • Ruby regex for matching simpliest Ruby's regexes
  • Why is ordered choice in pyparsing failing for my use case?
  • Support of :after in IE7
  • How to change user identity when git pushing via ssh?
  • Google App Engine Datastore: Dealing with eventual consistency
  • Bitrate JWplayer
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java