asimes asimes - 4 years ago 83
C++ Question

Is std::array safely returnable

Is the below sample function safe from the caller's point of view?

std::array<T, SIZE> foo() {
std::array<T, SIZE> bar;
// Initialization of bar members...
return bar;
}


I am aware that built-in arrays are not safely returnable but I am unsure if
std::array
is safely returnable. If it is, how is this accomplished?

Answer Source

This is fine. Returning bar from foo() makes a copy.

std::array is a struct that has an array member. When such a struct is copied, the array is also copied. In other words, the rules differ for bare arrays and arrays contained in structs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download