How Should the std::initializer_list Be Passed to a Function?

closed account (zb0S216C)
Should it be passed by reference, constant-reference, or value? I get the feeling that it should be passed by reference.

Wazzak
Last edited on
Non-const reference would be an error.
You could probably use a const reference, but the normal practice is by value.

Remember, it's only a lightweight view into an array allocated elsewhere, including, possibly static read-only memory.
Last edited on
closed account (zb0S216C)
@Cubbi: As you've pointed out, the std::initializer_list is most likely an array, so I think I'll go with constant-reference when passing.

Thanks, Cubbi :)

Wazzak
It's not an array, it's a view. A pointer and a size or two pointers. Assuming your use case is list initialization of a function argument, reference is just unnecessary overhead. Granted, a compiler that can do list unit, will optimize that reference out. It just looks unusual.
Last edited on
Topic archived. No new replies allowed.