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?
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.