default-arguments

What would be a proper invalid value for a pointer?

风格不统一 提交于 2019-12-02 07:05:06
Suppose I have this code. Your basic "if the caller doesn't provide a value, calculate value" scenario. void fun(const char* ptr = NULL) { if (ptr==NULL) { // calculate what ptr value should be } // now handle ptr normally } and call this with either fun(); // don't know the value yet, let fun work it out or fun(something); // use this value However, as it turns out, ptr can have all kinds of values, including NULL, so I can't use NULL as a signal that the caller doesn't provide ptr. So I'm not sure what default value to give ptr now instead of NULL. What magic value can I use? Does anybody

Why are these default arguments allowed?

[亡魂溺海] 提交于 2019-12-01 18:10:25
问题 I've found this question, and I'm completely baffled. The answer says b is invalid, "Non-static members can not be used as default arguments.". That makes perfect sense. What I don't understand is why the other two are okay. In fact, I'm struggling to understand what the semantics are if the default is not a constant expression... What's going on here? Default parameters are clearly evaluated at compile time. Does the compiler simply pick the current value? #include <iostream> int g_x = 44;

Why are these default arguments allowed?

懵懂的女人 提交于 2019-12-01 17:56:41
I've found this question, and I'm completely baffled. The answer says b is invalid, "Non-static members can not be used as default arguments.". That makes perfect sense. What I don't understand is why the other two are okay. In fact, I'm struggling to understand what the semantics are if the default is not a constant expression... What's going on here? Default parameters are clearly evaluated at compile time. Does the compiler simply pick the current value? #include <iostream> int g_x = 44; struct Foo { int m_x; static int s_x; Foo(int x) : m_x(x) {} int a(int x = g_x) { return x + 1; } int b

Fortran 2003 / 2008: Elegant default arguments?

懵懂的女人 提交于 2019-11-30 13:13:25
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 SUB CALL SUB(3) CONTAINS SUBROUTINE SUB(VAL) INTEGER, OPTIONAL :: VAL INTEGER :: AVAL ! short for "actual val" IF(PRESENT(VAL)) THEN AVAL = VAL ELSE AVAL = -1 ! default value END IF WRITE(*,'("AVAL is ", I0)') AVAL END SUBROUTINE SUB END PROGRAM PDEFAULT Personally, I often ran into the problem of accidentially typing VAL instead of AVAL , i.e. the

Python: Default list in function

不想你离开。 提交于 2019-11-30 09:44:33
问题 From the Summerfield's Programming in Python3: it says as follows: when default values are given, they are created at the time the def statement is executed, not when the function is called. But my question is for the following example: def append_if_even(x, lst =None): lst = [] if lst is None else lst if x % 2 ==0: lst.append(x) return lst As the first time definition executed, lst is point to None But after the function call append_if_even(2), Shouldn't lst point to [2], since after lst

Python: Default list in function

感情迁移 提交于 2019-11-29 16:48:59
From the Summerfield's Programming in Python3: it says as follows: when default values are given, they are created at the time the def statement is executed, not when the function is called. But my question is for the following example: def append_if_even(x, lst =None): lst = [] if lst is None else lst if x % 2 ==0: lst.append(x) return lst As the first time definition executed, lst is point to None But after the function call append_if_even(2), Shouldn't lst point to [2], since after lst.append(x) lst not point to None anymore ? Why the next execution still make lst point to none? What really

C++ Default Argument Error

眉间皱痕 提交于 2019-11-29 14:01:16
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 ComplexNumber::print(std::ostream&) const’ Here is my code at those certain areas: ComplexNumber.cpp 21 void ComplexNumber::print(ostream & out = cout) const { ComplexNumber.h 17 void print(ostream & out = cout) const; You should only specify the default parameter in the function declaration, i.e. in the header. You implementation should look something like

function overloading vs default argument in c++

假装没事ソ 提交于 2019-11-29 08:42:27
Hi i have a confusion or to say more i need to understand something. I have a procedure and another overloaded procedure of same. string conct (string a, string b) { string str = conct(a, b, "string"); return str; } string conct (string a, string b, const char* c) { // do the processing; return concatenated_string; } is it possible that instead of having two overloaded functions, if i make c in the overloaded function as default argument. So that even if someone passes only two arguments, i can just have one function to handle that case. But my main concern comes in the third argument which is

Default argument vs overloads in C++

十年热恋 提交于 2019-11-29 03:17:46
For example, instead of void shared_ptr::reset() noexcept; template <typename Y> void shared_ptr::reset(Y* ptr); one may think of template <typename Y = T> void shared_ptr::reset(Y* ptr = nullptr); I think performance difference is negligible here, and the second version is more concise. Is there any specific reason the C++ standard goes the first way? The same question has been asked for the Kotlin language, and default argument is preferred there. Update: std::unique_ptr::reset() follows the default argument design (see here ). So I think the reason std::shared_ptr::reset() uses overloads is

error: default argument given for parameter 1

痞子三分冷 提交于 2019-11-28 16:56:05
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; }; First I thought that default parameters are not allowed as a first parameter in C++ but it is allowed. Yacoby You are probably redefining the default parameter in the implementation of the function. It should only be defined in the function declaration. //bad (this won't compile) string Money::asString(bool shortVersion=true){ } //good (The default parameter is commented out, but you can remove it totally