0

C99 compound literals are not supported in C++. In many cases, list intialization provides an excellent alternative. However, they are not guaranteed not to heap-allocate memory.

Are there any convenient and portable alternatives for C++ code that needs to work entirely without heap allocations?

gspr
  • 11,144
  • 3
  • 41
  • 74
  • List initialization and `std::initializer_list` are two different things. List initialization *might* lead to the creation of an `std::initializer_list` object, but it all depends on the object you initialize and the constructors defined for its class. – Some programmer dude Sep 01 '20 at 11:30

2 Answers2

2

As the answers on the linked question indicate, since C++14 the compiler cannot heap-allocate an initializer list. Moreover, even pre-C++14 compilers won't heap allocate initializer lists, as there's absolutely no reason to do so (and plenty of reasons not to).

Sneftel
  • 40,271
  • 12
  • 71
  • 104
1

If your program works entirely without heap allocations, adding list initialization will be fine and will not cause heap memory allocation.

The cases where iist initialization might cause heap allocation are the cases where your objects already use heap memory, for example std::list. You certainly don't use those, if you are heap free.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42