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
7
votes
1 answer

Default argument using curly braces initializer

I have this snippet of code which seems to work well: class foo{/* some member variables and functions*/}; void do_somthing(foo x={}){} int main(){ do_somthing(); } I used to use void do_somthing(foo x=foo()){} to default the x argument but I see…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
7
votes
2 answers

Interaction between default arguments and parameter pack (GCC and clang disagree)

I expect the following code to compile: #include template void print(T val = T{}, Args... args) { std::cout << val << ' ' << sizeof...(args) << std::endl; } int main() { print(); …
Lingxi
  • 14,579
  • 2
  • 37
  • 93
7
votes
2 answers

Why I cannot use previous argument values to define argument default values?

For example, why I cannot write this: void f(double x, double y = x); to declare a function f, for which the call f(x) is equivalent to f(x,x)? In case this doesn't seem useful to you, here's a possible usage scenario. In this example, I declare f…
a06e
  • 18,594
  • 33
  • 93
  • 169
7
votes
2 answers

Inheriting constructors w / wo their default arguments?

C++ Primer (5th edition) on page 629 states: If a base class constructor has default arguments, those arguments are not inherited. I tried this for myself and to me it seems that the derived constructor generated by the compiler also has the same…
Mugurel
  • 1,829
  • 3
  • 17
  • 26
7
votes
3 answers

Error: default argument given for parameter after previous specification

very simple task for me here and I'm not sure why this is giving me problems, I'm simply making two mockup classes try to compile without any logic in their methods whatsoever using headers and declarations already given to me. Honestly this is just…
user1438611
6
votes
1 answer

Way to Only Pass Second Template Parameter

So I have a template function which has a defaulted 2nd argument. It's 1st argument can be deduced, so something like: template void foo(const F param) This works fine in the general case, I'll just call foo(bar). But…
6
votes
2 answers

Is there a more pythonic way to have multiple defaults arguments to a class?

I have a class with many defaults since I can't function overload. Is there a better way than having multiple default arguments, or using kwargs? I thought about passing a dictionary in to my class but then how do I control whether or not the…
johnramsden
  • 291
  • 5
  • 14
6
votes
3 answers

How are method default arguments overridden in python?

Method default arguments can be overridden apparently: >>> class B: ... def meth(self, r=True): print r >>> class D(B): ... def meth(self, r=False): print r ... D().meth() False >>> B().meth() True How is this possible ? Is it considered…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
6
votes
1 answer

R: How default argument is passed to a function?

In the .GlobalEnv, I defined the following variable and function x = 0; foo <- function(t=x) {x=1; t} When I called the function in the following ways foo() # gives 1 foo(t=x) # gives 0 Can anyone help explain this? Thanks!!!
Eric
  • 253
  • 1
  • 3
  • 7
5
votes
1 answer

Is there a way to have dynamic default arguments?

I'm trying to make a class where the user can modify member variables to change the default arguments of its member functions. class Class { public int Member; public void Method(int Argument = Member) { // This compiles fine,…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
5
votes
2 answers

What does this passage from cppreference.com (Default arguments) mean?

From cppreference page on default arguments: Non-static class members are not allowed in default arguments (even if they are not evaluated), except when used to form a pointer-to-member or in a member access expression: int b; class X { int a; …
LandP
  • 173
  • 6
5
votes
2 answers

function with default arguments as an argument

So, I know that you can pass a function as an argument like so: int a(int x) { return x + 1; } int b(int (*f)(int), int x) { return f(x); // returns x + 1 } I also know you can have a function with default arguments, like so: int a(int x =…
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
5
votes
1 answer

Preventing instantiation of unused default function arguments

Lets say I have a function which takes a function pointer as a parameter, and that parameter has a default argument. template T* default_construct() { return new T(); } template void register(T* (*construct)() =…
Alex
  • 14,973
  • 13
  • 59
  • 94
5
votes
1 answer

Default argument and empty list initialization

Consider the following code, of a simple class with a constructor taking an argument with a default value. // Version 1 template struct object1 { using type = T; constexpr object1(const type& val = type()): value(val) {} type…
Vincent
  • 57,703
  • 61
  • 205
  • 388
5
votes
1 answer

Redefinition of default argument

I have encounter an error saying "redefinition of default argument" while I am compiling some codes. This is the line that is throwing the error: bool wCommandDistributor::initialise (const bool server = true, const short ncmds=0, …
user1744424
  • 101
  • 2
  • 7