Ik heb niet veel ervaring met initializer_list
s, maar de standaard lijkt te suggereren dat de implementatie van een initializer_list
lijkt op een paar aanwijzers naar een array. De lijst op getInitializer
heeft automatische levensduur , en dat geldt ook voor de array die het ondersteunt. Uiteindelijk krijg je een paar wijzers terug naar een array die niet meer bestaat.
De relevante delen van de standaard zijn 8.5.4 [decl.init.list] items 5 en 6:
5.- An object of type std::initializer_list
is constructed from an initializer list as if the implementation allocated an array of N elements of type E
, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and the std::initializer_list
object is constructed to refer to that array. If a narrowing conversion is required to initialize any of the elements, the program is ill-formed.
6.- The lifetime of the array is the same as that of the initializer_list
object.
Dus voor uw specifieke geval zou de implementatie ongeveer hetzelfde zijn als dit:
std::initializer_list> getInitializer() {
std::function __a[1] = {
[]() -> std::string {
return "If";
}
};
return std::initializer_list>(__a, __a+1);
}