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 885397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:54:22+00:00 2026-05-15T12:54:22+00:00

I have a function, void test( vector<int>& vec ); How can I set the

  • 0

I have a function,

void test( vector<int>& vec );

How can I set the default argument for vec ?
I have tried

void test( vector<int>& vec = vector<int>() );

But there’s a warning “nonstandard extension used : ‘default argument’ : conversion from ‘std::vector<_Ty>’ to ‘std::vector<_Ty> &'”

Is there a better way to do this ? Instead of

void test() {
    vector<int> dummy;
    test( dummy );
}

Regards,
Voteforpedro

  • 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-15T12:54:23+00:00Added an answer on May 15, 2026 at 12:54 pm

    Have you tried:

    void test(const vector<int>& vec = vector<int>());
    

    C++ does not allow temporaries to be bound to non-const references.

    If you really to need to have a vector<int>& (not a const one), you can declare a static instance and use it as a default (thus non-temporary) value.

    static vector<int> DEFAULT_VECTOR;
    
    void test(vector<int>& vec = DEFAULT_VECTOR);
    

    But beware, because DEFAULT_VECTOR will (can) be modified and won’t reset on each call ! Not sure that this is what you really want.


    Thanks to stinky472, here is a thread-safe alternative:

    Instead of providing a default value, you might as well overload test() with a zero-parameter version which calls the other version:

    void test()
    {
      vector<int> vec;
      test(vec);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function: void func(int a, int b, char s, FILE *fp, int
Suppose I have a function void myFun(int*) In C++ what exactly does the following
In C++, I have a function: void MyFunction(int p) { p=5; } Assume, I
I have the following function void initBoard(int * board[BOARD_ROWS][BOARD_COLS]){ int z = 0; for(
I have a simple function: void licz() { int result = 0; for (int
Suppose I have a C function: void myFunction(..., int nObs){ int myVec[nObs] ; ...
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
I have this function: void ToUpper(char * S) { while (*S!=0) { *S=(*S >=
I have a function void foo() and inside foo I call the function void
I have a function with void * as one of its parameters (the void

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.