Function default argument value depending on argument name in C duplicate

0 votes

If one defines a new variable in C++, then the name of the variable can be used in the initialization expression, for example:

int x = sizeof(x);

And what about default value of a function argument? Is it allowed there to reference the argument by its name? For example:

void f(int y = sizeof(y)) {}

This function is accepted in Clang, but rejected in GCC with the error:

'y' was not declared in this scope
Jun 2, 2022 in C++ by Nicholas
• 7,760 points
609 views

1 answer to this question.

0 votes

When the function is called with no argument for the corresponding parameter, the default argument is evaluated. 

In a default argument, a parameter must not appear as a potentially evaluated expression. 

A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name.

It provides the following example:

int h(int a, int b = sizeof(a)); // OK, unevaluated operand

So, this function declaration

void f(int y = sizeof(y)) {}

is correct because, according to C++17 8.3.3 Sizeof:, y is not an evaluated operand in this expression sizeof(y).

1 The size-of operator returns the number of bytes in the operand's object representation. 

The operand is either an expression or a parenthesized type-id, which is an unevaluated operand (Clause 8).

and C++17 6.3.2 Declaration Point:

1 Except as noted below, a name's point of declaration is immediately after its complete declarator (Clause 11) and before its initializer (if any).

answered Jun 7, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

functions in c++ call by value and call by reference

The code below shows how to call a function in both methods.  Please explain the major differences or meanings of call by value and call by reference to me.  1.Make a value-based call.  2.Call based on a reference.  The call by value method is demonstrated in the following code. In a comment, I expressed my reservations. #include<iostream> int main(){ void change(int);//why function prototype is before ...READ MORE

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
510 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
476 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
2,537 views
0 votes
1 answer

How does #include <bits/stdc++.h> work in C++? [duplicate]

#include <bits/stdc++.h> is a precompiled header implementation ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
8,594 views
0 votes
1 answer

What does "collect2: error: ld returned 1 exit status" mean?

 The ld returned 1 exit status error ...READ MORE

answered Feb 22, 2022 in Others by Aditya
• 7,680 points
82,065 views
0 votes
1 answer

C++ string in classes

The std namespace contains the string class. You need change the class to something like this: class Language { public: ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,236 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
704 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,429 views
0 votes
1 answer

The new syntax "= default" in C++11

A defaulted default function Object() { [native code] } is defined as a user-defined default function Object() { [native code] } with an empty compound statement and no initialization list. I'll give you an example to demonstrate the difference: #include <iostream> using namespace std; class A { public: ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
495 views
0 votes
1 answer

functions in c++ call by value and call by reference

Calling a function by value copies the argument and stores it in a local variable for use by the function, so the argument is unaffected if the value of the local variable changes.  The argument is passed to the function as a reference rather than a copy, so if the function changes the value of the argument, the argument is also changed.   The void change(int); function prototype informs the compiler that there is a function named change that takes a single int argument and returns void (i.e. nothing).  Because there is no & with the argument, it is called by value.  You have the line change(orig); later in your code, which actually calls the function with. Take a look at the output of ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
639 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP