Opened 7 years ago

#11670 new Bugs

Boost intrusive constructors should be constexpr

Reported by: ben.maurer@… Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: intrusive
Version: Boost 1.60.0 Severity: Problem
Keywords: Cc:

Description

Boost intrusive constructors should be marked constexpr. Doing so would allow for their use in static variables without SIOF. As an example consider this program that uses a simple linked list

http://ideone.com/O5YWM8

because the LinkedList constructor is not constexpr, the variable gets initialized in file declaration order -- after my_crazy_variable. Therefore even though my_crazy_variable's ctor pushes something on to the list, it gets lost when the list is initialized and the program crashes due to being unable to pop a value.

However, if you declare LinkedList's constructor constexpr the list is initialized in the zero initialization phase (http://en.cppreference.com/w/cpp/language/initialization#Non-local_variables). This means that the linked list can be used in other constructors without worrying about initialization order.

While my example is obviously not a sane use case, imagine a linked list that registers all live Widget objects. if somebody creates a widget during static initialization they could encounter the same problem.

A side benefit to doing this is that code size is reduced as the code to initialize the linked list is not created

Change History (0)

Note: See TracTickets for help on using tickets.