Advertisement Jump to content
Sign in to follow this  

std::initializer list as array

This topic is 1824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Was cleaning up some older code and wanted to add an initializer_list to a simple wrapper class.  The data from the list is to be passed to an API function that accepts a pointer and length.


I was thinking of doing something like:


Is this 'kosher'?  I'd like to avoid having to create a temporary store to copy the data.


Or perhaps just:


would work? states that the iterator type is a pointer.

Edited by Ryan_001

Share this post

Link to post
Share on other sites

That's going to work only if your data is contiguous in memory.

If your list container is an std::list, then the data is not continuous, and your example wont work.. An std::vector will work as expected, but in that case you should use or &vec[0].

On the other hand, you could pass in the iterator itself (which acts as a pointer as far as dereferencing goes, but is not one, you can't pass an iterator where a pointer is expected), and then inside the function use it++ to move forward no matter the container type until you've done it container.size() times.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!