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

  • SEARCH
  • Home
  • 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 9010715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:32:13+00:00 2026-06-16T02:32:13+00:00

Is it possible to prevent array-to-pointer decay in arguments expanded from a parameter pack?

  • 0

Is it possible to prevent array-to-pointer decay in arguments expanded from a parameter pack?

For example:

#include <iostream>

void foo() {
  std::cout << "empty\n";
}

template <typename T, typename... Rest>
void foo(T &&t, Rest... rest) {
  std::cout << "T, ...\n";
  foo(rest...);
}

template <typename... Rest>
void foo(char *p, Rest... rest) {
  std::cout << "char*, ...\n";
  foo(rest...);
}

template <int N, typename... Rest>
void foo(char (&first)[N], Rest... rest) {
  std::cout << "char[], ...\n";
  foo(rest...);
}

int main() {
  char a[2], b[2], c[2];
  foo(a, b, c);
}

…outputs:

char[], ...
char*, ...
char*, ...
empty

As you can see, the first call goes to the array-based overload, but subsequent calls go to the pointer-based overload. Is there any way to get all of the calls to go to the array-based overload?

Related: Problems specializing variable template function

  • 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-06-16T02:32:15+00:00Added an answer on June 16, 2026 at 2:32 am

    You want to pass the parameter pack by rvalue reference:

    void foo(char (&first)[N], Rest&&... rest)
                                   ^^
    

    Although it’s not exactly related to the question at hand, when you forward arguments, you also want to use std::forward. So with those modifications, the code looks something like this overall:

    #include <iostream>
    
    void foo()
    {
        std::cout << "empty\n";
    }
    
    template <typename T, typename... Rest>
    void foo(T&& t, Rest... rest)
    {
        std::cout << "T, ...\n";
        foo(std::forward<Rest>(rest)...);
    }
    
    template <typename... Rest>
    void foo(char const* p, Rest... rest)
    {
        std::cout << "char*, ...\n";
        foo(std::forward<Rest>(rest)...);
    }
    
    template <int N, typename... Rest>
    void foo(char (&first)[N], Rest&&... rest)
    {
        std::cout << "char[], ...\n";
        foo(std::forward<Rest>(rest)...);
    }
    
    int main()
    {
        char a[2], b[2], c[2];
        foo(a, b, c);
    }
    

    Giving the result:

    char[], ...
    char[], ...
    char[], ...
    empty
    

    I haven’t changed the other overloads to do the same, but you’d normally want them to use an rvalue reference as well (if they were actually being used).

    As to why you’d want to do this/why it works: an rvalue reference template parameter can bind to either an rvalue or an lvalue. The crucial point we care about here is that when it binds to an lvalue, it remains an lvalue. In the case of an array, it retains its identity as an array, so what’s received is an array.

    When/if we pass an array by value, it undergoes the normal "decay" to a pointer, just like with a normal function.

    For this specific case, we could also use a normal lvalue reference — but if we did, that would not work for any type that wasn’t an lvalue. For example, if we tried to call foo(1,2,3);, we’d get an error because an lvalue reference can’t bind to 1, 2 or 3. To deal with that we could pass a const lvalue reference, but then we wouldn’t be binding the reference directly to the rvalue — we’d be creating a temporary containing a copy of the rvalue that was passed, and then binding the lvalue reference to that temporary copy instead. For the specific case of an int, that probably wouldn’t be a major problem, but with something that was more expensive to copy (or if we wanted access to the original, not a copy) that could be a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is is possible to prevent Wget from making an output file when there is
Is it possible to prevent a newer SVN client (say 1.6) from automatically upgrading
Is it possible to prevent user from writing letters to a textbox (i.e. force
Very straightforward: Is it possible to prevent the browser from scrolling when a user
Does anyone know if it is possible to prevent a work item from being
I'm wondering if it's possible for a view to prevent itself from opening as
Possible Duplicate: How can I prevent SQL injection in PHP? This is the example
Possible Duplicate: Prevent any form of page refresh using jQuery/Javascript how can i prevent
two little questions regarding the UISearchBar/SearchDisplayController: Is it possible to prevent/hide the overlay which
I want to prevent sending file on hotmail/gmail accounts. Is it possible to track

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.