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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:37:37+00:00 2026-05-23T02:37:37+00:00

struct X { X():mem(42){} void f(int param = mem) //ERROR { //do something }

  • 0
struct X
{
   X():mem(42){}
   void f(int param = mem) //ERROR
   {
      //do something
   }
private: 
   int mem;
};

Can anyone give me just one reason as to why this is illegal in C++?! That is to say, I know that it is an error, I know what the error means, I just can’t understand why would this be illegal!

  • 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-23T02:37:38+00:00Added an answer on May 23, 2026 at 2:37 am

    Your code (simplified):

    struct X
    {
       int mem;
       void f(int param = mem); //ERROR
    };
    

    You want to use a non-static member data as default value for a parameter of a member function. The first question which comes to mind is this : which specific instance of the class the default value mem belongs to?

    X x1 = {100};  //mem = 100
    X x2 = {200};  //mem = 200
    
    x1.f(); //param is 100 or 200? or something else?
    

    Your answer might be 100 as f() is invoked on the object x1 which has mem = 100. If so, then it requires the implementation to implement f() as:

    void f(X* this, int param = this->mem);
    

    which in turn requires the first argument to be initialized first before initialization of other argument. But the C++ standard doesn’t specify any initialization order of the function arguments. Hence that isn’t allowed. Its for the same reason that C++ Standard doesn’t allow even this:

    int f(int a, int b = a); //§8.3.6/9
    

    In fact, §8.3.6/9 explicitly says,

    Default arguments are evaluated each
    time the function is called. The order
    of evaluation of function arguments is
    unspecified
    . Consequently, parameters
    of a function shall not be used in
    default argument expressions
    , even if
    they are not evaluated.

    And rest of the section is an interesting read.


    An interesting topic related to “default” arguments (not related to this topic though):

    • Default argument in the middle of parameter list?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct like this: struct A { void i(int i) {} void
struct elem { int i; char k; }; elem user; // compile error! struct
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
struct { char a; int b; } x; Why would one define a struct
struct Drink { public string Name { get; private set; } public int Popularity
Firstly, I had a snippet as following: struct D { int sum; D():sum(0){accum();} void
struct SomeStruct { int a; int b; }; SomeStruct someFn( int init ) {
struct Div { int i; int j; }; class A { public: A(); Div&
struct struct0 { int a; }; struct struct1 { struct struct0 structure0; int b;
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; 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.