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.