Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7443287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:18:28+00:00 2026-05-29T11:18:28+00:00

I know how to write a variadic template function that accepts a variable number

  • 0

I know how to write a variadic template function that accepts a variable number of arguments:

template<int I, typename... Rest>
void f() {
    // whatever
}

and I know how to write a template function that accepts a reference to an array:

template<typename T, unsigned int Length>
void f(T(&arr)[Length]) {
    // whatever
}

but I cannot think of how to combine the two so that the function accepts a variable number of references to arrays.

My first attempt was

template<typename T, unsigned int Length>
unsigned int arrlen(T(&)[Length]) {
    return Length;
}

template<typename T, unsigned int Length>
int f(T(&arr)[Length]) {
    return Length;
}

template<typename T, unsigned int Length, typename... Rest>
int f(T(&arr)[Length], Rest... rest) {
    return Length + f(rest...);
}

int main() {
    int a[] = {1 , 2, 3}, b[] = {1, 2, 3, 4, 5}, c[] = {1};

    cout << f(a, b, c);
}

But the compiler tells me

a.cpp: In function ‘int f(T (&)[Length], Rest …) [with T = int, unsigned int Length = 3u, Rest = {int*, int*}]’

a.cpp:23:22: instantiated from here

a.cpp:17:27: error: no matching function for call to ‘f(int*&, int*&)’

a.cpp:17:27: note: candidates are:

a.cpp:11:22: note: template int f(T(&)[Length])

a.cpp:16:5: note: template int f(T(&)[Length], Rest …)

So I was thinking that you could write an object that would store the length of the array it was constructed with, and then pass a variable number of those (which would be implicitly constructed from the arrays passed) to the function. Here is my try at that:

template<typename T, unsigned int Length>
struct Array {
    Array(T(&arr)[Length]) : arr(arr), len(Length) { }

    T(&arr)[Length];
    const unsigned int len;
};

int f() {
    return 0;
}

template<typename T, unsigned int Length, typename... Args>
int f(const Array<T, Length>& a1, Args... rest) {
    return a1.len + f(rest...);
}

int main() {
    int a[] = { 1, 2, 3 }, b[] = { 1, 2, 3, 4, 5 }, c[] = { 1 };

    cout << f(a, b, c);
}

But when I try to compile it with GCC 4.6.1 I get the error

a.cpp: In function ‘int main()’:

a.cpp:27:22: error: no matching function for call to ‘f(int [3], int [5], int [1])’

a.cpp:27:22: note: candidates are:

a.cpp:16:47: note: template int f(const Array&, Args …)

a.cpp:20:5: note: int f()

a.cpp:20:5: note: candidate expects 0 arguments, 3 provided

However, besides fixing the second code (which is more of a workaround for not knowing how to do what I really want to do), the actual point of this question and the thing I would actually like to learn is how to do this without using proxy objects like that if possible, like the first code. So how can this be done? Was there just a simple syntax error in one of the tries I posted?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:18:28+00:00Added an answer on May 29, 2026 at 11:18 am

    If you just want to sum up the length of a number of arrays, you can do that directly:

    template<typename T, unsigned int Length>
    int f(const T (&)[Length]) {
        return Length;
    }
    
    template<typename T, unsigned int Length, typename... Args>
    int f(const T (&)[Length], Args&... rest) {
        return Length + f(rest...);
    }
    
    int main() {
        int a[] = { 1, 2, 3 }, b[] = { 1, 2, 3, 4, 5 }, c[] = { 1 };
    
        std::cout << f(a, b, c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know i could write a custom function that does just this, but it
If I would like to write a method that takes a variable number of
So I know I can write my own HTML-encoding function like this: function getHTMLEncode(t)
I know that with mysql you can write SQL statements into a .sql file
Anyone know if it is possible to write an app that uses the Java
Let's say I have a variadic function foo(int tmp, ...) , when calling foo
I don't know if this can be achieved through variadic template, variadic marcos or
I know how to write a basic C Mex function with one output of
There are at least two ways that I know of to write a Symbian
I know I could write an interposer to watch the arguments being passed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.