default-arguments

Default argument using curly braces initializer

安稳与你 提交于 2020-01-22 20:37:06
问题 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 this way ={} in some book or online example(can not remember). Is it totally ok to use it? Is there any difference between the two methods? 回答1: foo x=foo() is copy initialization, Initializes an object from another object and foo() is value

default argument promotion of pointer

我与影子孤独终老i 提交于 2020-01-14 03:06:30
问题 Which roles are used when i call a function with pointer argument unless declare the function before? I know that float are promoted to double and that are executed the integral promotion, but what happens at the pointers? 回答1: Nothing. Default argument promotions don't apply to arguments of pointer type so they are left unchanged in type and value. 来源: https://stackoverflow.com/questions/14068058/default-argument-promotion-of-pointer

Python Default Arguments Evaluation [duplicate]

百般思念 提交于 2020-01-13 06:30:09
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (32 answers) Closed 4 years ago . I was reading the python tutorial from Python Documentation Release 2.7.10 and I came across something like this. Code def fun1(a,L=[]): L.append(a) return L print fun1(1) print fun1(2) print fun1(3) def fun2(a,L = None): if L is None: L=[] L.append(a) return L print fun2(1) print fun2(2) print fun2(3) Output [1] [1, 2] [1, 2, 3] [1] [2] [3] Process finished with

Python Default Arguments Evaluation [duplicate]

…衆ロ難τιáo~ 提交于 2020-01-13 06:30:08
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (32 answers) Closed 4 years ago . I was reading the python tutorial from Python Documentation Release 2.7.10 and I came across something like this. Code def fun1(a,L=[]): L.append(a) return L print fun1(1) print fun1(2) print fun1(3) def fun2(a,L = None): if L is None: L=[] L.append(a) return L print fun2(1) print fun2(2) print fun2(3) Output [1] [1, 2] [1, 2, 3] [1] [2] [3] Process finished with

Missing default argument - compiler error

谁都会走 提交于 2020-01-11 17:09:31
问题 void func ( string word = "hello", int b ) { // some jobs } in another function //calling func ( "", 10 ) ; When I have compiled it, compiler emits error ; default argument missing for parameter How can I fix it without changing anything, of course, such as not making "int b = 0" ? Moreover, I want use that function like func ( 10 ) or func ( "hi" ) ? Is my compiler not do its job, properly ? 回答1: You can't have non-default parameters after your default parameters begin. Put another way, how

Missing default argument - compiler error

折月煮酒 提交于 2020-01-11 17:09:30
问题 void func ( string word = "hello", int b ) { // some jobs } in another function //calling func ( "", 10 ) ; When I have compiled it, compiler emits error ; default argument missing for parameter How can I fix it without changing anything, of course, such as not making "int b = 0" ? Moreover, I want use that function like func ( 10 ) or func ( "hi" ) ? Is my compiler not do its job, properly ? 回答1: You can't have non-default parameters after your default parameters begin. Put another way, how

Fortran 2003 / 2008: Elegant default arguments?

空扰寡人 提交于 2020-01-10 01:35:57
问题 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

Will default arguments at an arbitrary point in the argument list ever be possible?

我怕爱的太早我们不能终老 提交于 2020-01-02 23:50:21
问题 Hopefully whoever reads this knows about default arguments: void setCase (string &str, int form = UPPERCASE) { for (char &c : str) c = (form == UPPERCASE ? c & ~0x20 : c | 0x20); //this bit differentiates english uppercase and lowercase letters } int main() { string s1 = "HeLlO", s2 = s1, s3 = s1; setCase (s1, UPPERCASE); //now "HELLO" setCase (s2, LOWERCASE); //now "hello" setCase (s3); //now "HELLO" due to default argument } The one drawback of using default arguments is that you have to

How to pass a default argument value of an instance member to a method?

徘徊边缘 提交于 2019-12-27 10:21:08
问题 I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the following error message: NameError: name 'self' is not defined I want the method to behave like this: C("abc").process() # prints "abc" C("abc").process("xyz") # prints "xyz" What is the problem here, why does this not work? And how could I make

How to pass a default argument value of an instance member to a method?

时光总嘲笑我的痴心妄想 提交于 2019-12-27 10:19:29
问题 I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the following error message: NameError: name 'self' is not defined I want the method to behave like this: C("abc").process() # prints "abc" C("abc").process("xyz") # prints "xyz" What is the problem here, why does this not work? And how could I make