Dan Stahlke(deleted)
03/30/2015 6:04 PM
post113639
|
The following one-liner gives an infinite loop:
#include <cstdio>
#include <initializer_list>
int main() {
for(int x : {10,20,30}) std::printf("x=%d\n", x);
}
The issue is that std::initializer_list<T>::_Last gets populated with the size of the list (3 in this case) rather than
with the end pointer. In other words, g++ sets _Last=3 whereas it should be _Last=_First+3.
There is a workaround:
#include <cstdio>
#include <cstddef>
#include <initializer_list>
template <typename T>
std::initializer_list<T> foo(const std::initializer_list<T> &il) {
#if __QNX__
if(il.end() < il.begin()) {
return std::initializer_list<T>(il.begin(), std::size_t(il.end()));
} else {
return il;
}
#else
return il;
#endif
}
int main() {
for(int x : foo({3,1,4,1,5,9})) {
std::printf("x=%d \n", x);
}
return 0;
}
|
|
|