Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - initializer_list bug: (4 Items)
   
initializer_list bug  
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;
}
Re: initializer_list bug  
Hi Dan, 

Thanks for the report. Attached is an initializer_list that should address the issue.

Regards,

Ryan Mansfield
Attachment: Text initializer_list 1.61 KB
Re: initializer_list bug  
Wow, that was a fast reply!  Yes, this fixes the issue.  Thank you.
Re: initializer_list bug  
For the benefit of others reading this thread,  the file to be replaced is "target/qnx6/usr/include/cpp/initializer_list
", not "target/qnx6/usr/include/c++/4.7.3/initializer_list".