7

I don't know if what I have is a compiler bug or if I just don't know the syntax for what I'm trying to do. Imagine a function that takes 2 array references:

void takeArrays(const char (&str1)[4], const char (&str2)[4]) {
}

This compiles fine when called with:

takeArrays("foo", "bar");

Why would I want to do this? Because passing const char* loses the size information of the string literal, and that's important for what I'm doing.

What I really want to do, however, is pass a variadic number of array references. This is where things get slightly ugly (it gets worse). I naively tried this:

template<typename... Args>
void takeArrays(const char (&Args... strs)[4]) {
}

And got "error: variable or field ‘takeArrays’ declared void" and "error: expected primary-expression before ‘const’" (gcc 4.6). So I tried this:

template<typename... Args>
void takeArrays(const char (&(Args... strs))[4]) {
}

And got "no matching function for call to ‘takeArrays(const char [4], const char [4])’" and "candidate is template<class ... Args> void takeArrays(const char (& (*)(Args ...))[4])". Which is unreadable but seems close to what I want. I've tried many variations and can't seem to get it to compile.

Assuming there's a proper way to write the above, what I really want to do is call:

takeArrays("foo", "foobar", "longerstring");

And get a variadic list of arrays of different sizes, i.e. the call above should be expanded by the compiler to:

void takeArrays(const char (&str1)[4], const char (&str2)[7],
                const char (&str3)[13]);

Which was the first think I tried doing, and my attempt was something like:

template<size_t... Sizes>
void takeArrays(const char (&strs)[Sizes]...);

Needless to say all I got was error messages. I'm aware that what I'm trying to do is a bit crazy but I really want to know if it's possible, and if so, what the appropriate syntax is. Thanks in advance.

1 Answer 1

10

The syntax for variadic arrays of the same element type is:

template<size_t... Sizes>
void takeArrays(const char (&...args)[Sizes]);

This is similar to the general variadic const reference syntax:

template<typename... Args>
void takeArrays(const Args &...args);

An easy way to remember is that the ellipsis goes immediately before the parameter name.

2
  • Yep, that worked. Thanks! I'll remember the ellipsis before parameter name trick. Out of curiosity, how would I write the original parameters of the same size? I tried template<typename...Args> void takeArrays(cont char Args (&... args([4]) and const char (Args &... args)[4] and those don't work. Nov 6, 2012 at 18:33
  • 5
    @ÁtilaNeves That can't be made to work for the same reason that you can't write a variadic function template taking multiple ints: template</* what to put here?? */> void foo(int... i /* doesn't work: no pack to expand here */);. The usual workaround would be to allow arrays of the same size with different element type, i.e. template<typename... T, std::size_t Size> void foo(T (&... args)[Size]);. You can then optionally add a static_assert to enforce that all T be const char.
    – Luc Danton
    Nov 6, 2012 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.