Yes , I managed to get the following work (as you allow something similar):
template<typename T, size_t N> void foo(array<T, N> src) { ... } ... foo('a', 'b'); foo(1, 2, 3);
Here's how:
#include <array> #include <iostream> #include <utility> using namespace std; template<typename T, unsigned long N> void foo(array<T,N> src) { for (auto e : src) cout << e << endl; } template<class T, class... Tail> auto make_array(T head, Tail... tail) -> std::array<T, 1 + sizeof...(Tail)> { std::array<T, 1 + sizeof...(Tail)> a = {{ head, tail ... }}; return a; } template<class T, class... Tail> void foo(T&& head, Tail&&... values) { foo(make_array(std::forward<T>(head), std::forward<Tail>(values)...)); } int main() { foo('a', 'b'); foo(1, 2, 3); }
I tested this with gcc 4.7.2 and with clang 3.4 (trunk 184647), they work as expected.
Here is the online version of Stacked-Crooked. However, this code does not compile in Ideone. Since I could not figure out the parameters passed to the compiler in Ideone, I abandoned this site.
I shamelessly stole the make_array function from @Pavel Minaev on How to emulate array initialization C "int arr [] = {e1, e2, e3, ...}" with the question std :: array? . Other make_array suggestions caused compilation errors that I could not fix.
This make_array function has limitations, please read the entire post ; specifically a discussion of std :: array - if only he knew its size in comp.lang.C ++. refers to the moderator. Obviously getting a sensible make_array quite difficult. I would not recommend a simple make_array approach in this answer, which will be used in production code.
You will have no problem if the size was a template argument for std::initializer_list . Hence the question Why is size not an argument to the std :: initializer_list template?