Questions tagged [default-arguments]

A default argument is a value passed to a certain parameter of a function if the user does not explicitly supply a value for that parameter.

A default argument is a value passed to a certain parameter of a function if the user does not explicitly supply a value for that parameter.

Some language extend this concept to other entities that have parameters. An example is C++, which has default template arguments, which is a value, type or template that is provided as an argument to a template parameter if the user does not explicitly provide an argument.

356 questions
27
votes
3 answers

Why can't the compiler deduce the template type from default arguments?

I was surprised the following code resulted in a could not deduce template argument for T error: struct foo { template void bar(int a, T b = 0.0f) { } }; int main() { foo a; a.bar(5); return 0; } Calling a.bar(5)…
Samaursa
  • 16,527
  • 21
  • 89
  • 160
26
votes
3 answers

Is it ok to return default argument's value by const reference?

Is it ok to return default argument's value by const reference like in the examples below: https://coliru.stacked-crooked.com/a/ff76e060a007723b #include const std::string& foo(const std::string& s = std::string("")) { return s; } int…
23
votes
5 answers

PHP function to set the default value as an object

A function (actually the constructor of another class) needs an object of class temp as argument. So I define interface itemp and include itemp $obj as the function argument. This is fine, and I must pass class temp objects to my function. But now I…
wadkar
  • 960
  • 2
  • 15
  • 29
22
votes
3 answers

Why is a parameter pack allowed after default arguments?

Maybe I'm missing something obvious, but the following compiles and runs, and I'm not sure why. I am aware of this, but in the example below the position of the parameter pack and the default argument are reversed. Doesn't it violate the rule that…
cantordust
  • 1,542
  • 13
  • 17
19
votes
7 answers

Fortran 2003 / 2008: Elegant default arguments?

In fortran, we can define default arguments. However, if an optional argument is not present, it can also not be set. When using arguments as keyword arguments with default values, this leads to awkward constructs like PROGRAM PDEFAULT CALL…
19
votes
3 answers

Is it possible to use member function call as default argument?

Here is my code: struct S { int f() { return 1; } int g(int arg = f()) { return arg; } }; int main() { S s; return s.g(); } This fails to compile with the error: error: cannot call member function 'int S::f()' without object Trying…
M.M
  • 138,810
  • 21
  • 208
  • 365
18
votes
5 answers

Unintuitive behaviour with struct initialization and default arguments

public struct Test { public double Val; public Test(double val = double.NaN) { Val = val; } public bool IsValid { get { return !double.IsNaN(Val); } } } Test myTest = new Test(); bool valid = myTest.IsValid; The above gives…
Ricibob
  • 7,505
  • 5
  • 46
  • 65
17
votes
3 answers

How to call a function with default parameter through a pointer to function that is the return of another function?

I have functions Mult, Add, Div, Sub, Mod those takes two integers and returns the result of its parameters. And a function Calc that takes a character as an Operator and returns a pointer to function that returns an integer and takes two integer…
Syfu_H
  • 605
  • 4
  • 17
17
votes
4 answers

Default argument vs overloads in C++

For example, instead of void shared_ptr::reset() noexcept; template void shared_ptr::reset(Y* ptr); one may think of template void shared_ptr::reset(Y* ptr = nullptr); I think performance difference is negligible…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
17
votes
4 answers

Skip some arguments in a C++ function?

I have a C++ function that has 5 arguments, all of which have default values. If I pass in the first three arguments, the program will assign a default value to the last two arguments. Is there any way to pass 3 arguments, and skip one in the…
kyrpav
  • 756
  • 1
  • 13
  • 43
16
votes
3 answers

At which point are default template arguments instantiated in C++?

Since every lambda expression in C++ has a unique closure type, and this type can be used as the default argument of a template parameter (since C++20, using decltype), it is important to know at which point that type is instantiated. Consider this…
Fedor
  • 17,146
  • 13
  • 40
  • 131
16
votes
4 answers

Get function's default value?

Is there a way to retrieve a function's default argument value in JavaScript? function foo(x = 5) { // things I do not control } Is there a way to get the default value of x here? Optimally, something like: getDefaultValues(foo); // {"x":…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
15
votes
3 answers

How to wrap a function with a varying number of default arguments to have only one argument?

I have a template function, let's call it the "client": template void client(T (*func)(const std::string&), const std::string& s) {} Then there are a number of "adaptee" functions that all have an identical type of the first,…
iksemyonov
  • 4,106
  • 1
  • 22
  • 42
15
votes
1 answer

C++: Adding and redefinition of default arguments in real world

There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example: void foo(int a, int b, int c = -1) { std::cout << "foo(" << a << ", " << b << ", " << c << ")\n"; } int main() { foo(1, 2); //…
DAle
  • 8,990
  • 2
  • 26
  • 45
15
votes
2 answers

Why default argument can't be added later in template functions?

C++ standard section 8.3.6.4 says that For non-template functions, default arguments can be added in later declarations of a function in the same scope. [...] But my question is that why it isn't allowed for template functions? What is the…
Destructor
  • 14,123
  • 11
  • 61
  • 126
1
2
3
23 24