Why does emplace_back
take a reference of the member that requires a definition? What is the difference between emplace_back(integer literal)
and <
As you said, emplace_back
takes arguments by reference, so passing base_trait::n
causes it to be odr-used.
an object is odr-used if its value is read (unless it is a compile time constant) or written, its address is taken, or a reference is bound to it;
Before C++17, that means the definiton of base_trait::n
is required here. But since C++17 the behavior changed, for constexpr static data member the out-of-class definition is not needed again.
If a const
non-inline (since C++17)
static data memberor a constexpr static data member (since C++11)
is odr-used, a definition at namespace scope is still required, but it cannot have an initializer.This definition is deprecated for constexpr data members (since C++17).
A static data member may be declared inline. An inline static data member can be defined in the class definition and may specify an initializer. It does not need an out-of-class definition. (since C++17)