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
9
votes
2 answers

Default arguments in Swift enums

I make SnapOperationQueue which is an operation queue with a few extensions I like. One of them is reprioritisation of groups of operations. An operation is added with one of these four priorities (SnapOperationQueuePriority): case Highest case…
niklassaers
  • 8,480
  • 20
  • 99
  • 146
9
votes
2 answers

template method and default template argument

My problem can be resumed by the following piece of code: template struct C2; template struct C1 { template class Container = C2> void m() {} }; template
lrleon
  • 2,610
  • 3
  • 25
  • 38
9
votes
1 answer

Am I using default arguments incorrectly?

I've just started going through a beginners book of C++. I have some java experience (but having said that, I've never used default arguments in java to be honest) So, as mentioned, my issue is with default arguments.. This is the code snippet I'm…
yoonsi
  • 754
  • 3
  • 10
  • 23
8
votes
4 answers

How can I cleanly specify which arguments I am passing and which remain default?

Asked because of this: Default argument in c++ Say I have a function such as this: void f(int p1=1, int p2=2, int p3=3, int p4=4); And I want to call it using only some of the arguments - the rest will be the defaults. Something like this would…
Pubby
  • 51,882
  • 13
  • 139
  • 180
8
votes
2 answers

Problem with bounded type parameterised case class and default args in Scala

Consider the following (tested with Scala 2.8.1 and 2.9.0): trait Animal class Dog extends Animal case class AnimalsList[A <: Animal](list:List[A] = List()) case class AnimalsMap[A <: Animal](map:Map[String,A] = Map()) val dogList =…
Kristian Domagala
  • 3,686
  • 20
  • 22
8
votes
1 answer

ES6 Named Object Parameter Destructuring

I am currently using the object destructuring pattern with default parameters described in that answer to ES6 Object Destructuring Default Parameters. (function test({a = "foo", b = "bar"} = {}) { console.log(a + " " + b); })(); I would like to…
8
votes
2 answers

Will unused default arguments decrease performance c++

Suppose I declare a function foo(int arg1, int arg2 = 0, int arg3 = 0, int arg4 = 0). The last three arguments will be specified only occasionally (if ever), and mostly, the funciton will be called as foo(some_int). Would I gain performance by…
Ludwik
  • 2,247
  • 2
  • 27
  • 45
8
votes
3 answers

Can I pass a default value to a reference for a std::string ?

void doStuff( std::string const & s1, std::string const & s2=""); I was wondering if this code is legal in C++, for the s2 string. I want to have a default argument, but passing a reference and having an empty string as default. Will a temporary be…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
8
votes
1 answer

C++ Default Argument Error

Any Idea why this error is coming up at compile time? ComplexNumber.cpp:21: error: default argument given for parameter 1 of ‘void ComplexNumber::print(std::ostream&) const’ ComplexNumber.h:17: error: after previous specification in ‘void…
Pat Murray
  • 4,125
  • 7
  • 28
  • 33
7
votes
4 answers

Why existing function arguments cannot be used to evaluate other default arguments?

I was writing a function foo() which takes 2 const char*s as arguments, pBegin and pEnd. foo() is passed a null terminated string. By default pEnd points to \0 (last character) of the string. void foo (const char *pBegin, const char *pEnd…
iammilind
  • 68,093
  • 33
  • 169
  • 336
7
votes
1 answer

Using {fmt} & source_location to create variadic-template-based logging function

I'd like to create a simple log function in C++ which prepend the code location to the log message. I'd like to avoid macros overall as well as the usage of __FILE__ & __LINE__. Note that the format string is always a compile-time string, and I'd…
galah92
  • 3,621
  • 2
  • 29
  • 55
7
votes
2 answers

Python: explicitly use default arguments

Under normal circumstances one calls a function with its default arguments by omitting those arguments. However if I'm generating arguments on the fly, omitting one isn't always easy or elegant. Is there a way to use a function's default argument…
user1492428
  • 145
  • 1
  • 8
7
votes
1 answer

Named Default Arguments in pybind11

I'm using pybind11 to wrap a C++ class method in a conversion lambda "shim" (I must do this because reasons). One of the method's arguments is defaulted in C++. class A { void meow(Eigen::Matrix4f optMat = Eigen::Matrix4f::Identity()); }; In…
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
7
votes
2 answers

Have I misunderstood the scope of this default argument shared_ptr?

Take a look at this: #include #include using Foo = int; using FooPtr = std::shared_ptr; FooPtr makeFoo() { FooPtr f{ new Foo(), [](Foo* ptr) { delete ptr; std::cerr << "!\n"; …
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
7
votes
1 answer

Why does clang emit these warnings?

The clang compiler emit warnings for the snippet below, as can be seen here. clang++ -std=c++14 -O0 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp:1:18: warning: braces around scalar initializer [-Wbraced-scalar-init] void point(int = {1},…
João Afonso
  • 1,934
  • 13
  • 19