default-arguments Questions

6

Solved

#include <iostream> int foo(int x = [](){ static int x = 0; return ++x; }()) { return x; }; int main() { std::cout << foo() << foo(); // prints "12", not "11&qu...
Coruscation asked 1/7, 2024 at 4:4

5

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, so...
Kriemhild asked 4/10, 2015 at 14:35

8

Solved

The C++20 feature std::source_location is used to capture information about the context in which a function is called. When I try to use it with a variadic template function, I encountered a probl...

9

Solved

It is a common mistake in Python to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger: >>> def ...
Rakeoff asked 6/2, 2012 at 9:50

1

Solved

Consider the following code: void foo(int* = 0); // (1) void foo(int = 0); // (2) int main() { foo(0); // OK, calls (2) foo(); // error, ambiguous overload } Both GCC and clang act like this, b...

3

Solved

I'm attempting to capture the this pointer within a lambda function that serves as the default parameter for a method. My goal is to invoke a method of this class from within the lambda, which nece...
Zoometry asked 8/9, 2023 at 8:20

3

Solved

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 w...
Pennipennie asked 16/8, 2021 at 7:10

4

Solved

Is there a way to specify default value in Go's function? I am trying to find this in the documentation but I can't find anything that specifies that this is even possible. func SaySomething(i str...
Capercaillie asked 26/10, 2013 at 22:10

1

Suppose I have function declarations like these: static const int R = 0; static const int I = 0; void f(const int& r = R); void g(int i = I); Per [dcl.fct.default]/1: If an initializer-claus...
Yellowgreen asked 21/9, 2022 at 12:53

7

Solved

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 aw...

6

Solved

If I am allowed to do the following: template <typename T = int> class Foo{ }; Why am I not allowed to do the following in main? Foo me; But I must specify the following: Foo<int&gt...
Trisyllable asked 12/3, 2013 at 22:52

2

Solved

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 me...
Flamboyant asked 30/1, 2022 at 15:34

2

Solved

I'm getting this error message with the code below: class Money { public: Money(float amount, int moneyType); string asString(bool shortVersion=true); private: float amount; int moneyType; }; ...
Refractive asked 30/3, 2010 at 13:53

2

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 ...
Erectile asked 8/12, 2021 at 21:36

2

Solved

I tried to write this function with a default template argument: template<typename A, typename B> void func(int i1, int i2, A a, B b = 123){ ... } In my mind I can call it like this: ...

4

Solved

I think the following code is very handy and no harmful: auto fn = [](bool b = false) -> int // NOT legal in C++11 { return b ? 1 : 0; }; Why does C++11 explicitly prohibit default argument...
Ibarra asked 7/1, 2013 at 3:44

5

Solved

The way to deal with mutable default arguments in Python is to set them to None. For example: def foo(bar=None): bar = [] if bar is None else bar return sorted(bar) If I type in the function def...
Aguila asked 4/6, 2021 at 1:52

3

Solved

Present signature is template<class TypeData,typename TypeFunc> bool isPrime(const TypeData& n,TypeFunc fSqrt,bool debug = false) and this works perfectly with std::cout<<(isPrime(...
Tourbillion asked 21/10, 2021 at 13:24

2

Solved

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 a...

5

Solved

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 mu...
Dermoid asked 15/8, 2011 at 12:36

1

Solved

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 f...
Epirus asked 25/2, 2021 at 21:16

1

Solved

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::Matrix...
Sanfred asked 29/7, 2019 at 5:11

3

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 <string> const std::string& fo...

2

I'm coming to Javascript from Python. In Python if you use a list or dictionary as a default argument for a function, every call sees the same object. So if you have a function like: def append_to...
Spirochete asked 9/9, 2019 at 19:33

2

Solved

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 ...
Freberg asked 18/3, 2016 at 8:48

© 2022 - 2025 — McMap. All rights reserved.