default-parameters

Is there anyway to use a member function as a default parameter?

空扰寡人 提交于 2019-12-10 15:35:17
问题 It tried something like this, which doesn't work. Is there a way to get a similar effect? class A { public: int foo(); void bar(int b = foo()); }; 回答1: Yes. Overload the function and call the member-function in it. void bar() { bar(foo()); } 来源: https://stackoverflow.com/questions/4901233/is-there-anyway-to-use-a-member-function-as-a-default-parameter

Default arguments and variadic functions

非 Y 不嫁゛ 提交于 2019-12-10 12:39:47
问题 Is there any way to specify a default parameter in a variadic function?(Applies to templates also) 回答1: In C++ you can replace the variadic function with one based on the Named Parameter Idiom. See the C++ FAQ item 10.20 What is the "Named Parameter Idiom"?. That gives you default functionality & convenient notation. Cheers & hth., 回答2: Why would you need both variadic and default params? For example, myFunc(int a=5, int b=5, int c=5); can receive 0-3 parameters with default values, and

Why C++ CLI has no default argument on managed types?

依然范特西╮ 提交于 2019-12-10 12:32:19
问题 The following line has the error Default argument is not allowed . public ref class SPlayerObj{ private: void k(int s = 0){ //ERROR } } Why C++ has no default argument on managed types ? I would like to know if there is a way to fix this. 回答1: It does have optional arguments, they just don't look the same way as the C++ syntax. Optional arguments are a language interop problem. It must be implemented by the language that makes the call, it generates the code that actually uses the default

Why is this default template parameter not allowed?

流过昼夜 提交于 2019-12-09 17:48:18
问题 I have the following class: template <typename Type = void> class AlignedMemory { public: AlignedMemory(size_t alignment, size_t size) : memptr_(0) { int iret(posix_memalign((void **)&memptr_, alignment, size)); if (iret) throw system_error("posix_memalign"); } virtual ~AlignedMemory() { free(memptr_); } operator Type *() const { return memptr_; } Type *operator->() const { return memptr_; } //operator Type &() { return *memptr_; } //Type &operator[](size_t index) const; private: Type *memptr

javaScript function - why my default argument fails?

别来无恙 提交于 2019-12-09 15:39:59
问题 My Javascript function leads my console to return me : TypeError: style is null Here the snippet: let style = { one: 1, two: 2, three: 3 } function styling(style = style, ...ruleSetStock) { return ruleSetStock.map(ruleSet => { console.log(ruleSet) return style[ruleSet] }) } console.log(styling(null, "one", "two", "three")) I can't understand why. It seems to me everything is great, Any hint would be great, thanks. 回答1: Default parameters is assigned only if no value or undefined is passed let

Why can't I use String.Empty as a default parameter value?

旧时模样 提交于 2019-12-08 17:23:00
问题 Today I was creating a default parameter value in a constructor. public SomeClass (String something = String.Empty) { // ... } The compiler complained. Default parameter value for "something" must be a compile-time constant. I was under the impression that Empty on the String class was a compile-time constant. .field public static initonly string Empty Am I missunderstanding the meaning of compile-time constant, or is it just more wackyness that I need to accept? 回答1: The accepted answer to

Closure (with default value) as function parameter [duplicate]

妖精的绣舞 提交于 2019-12-08 03:36:53
问题 This question already has answers here : Nil cannot be assigned to type ()->()? (3 answers) Swift 3 optional escaping closure parameter (5 answers) Closed 2 years ago . Thanks to the marvels of Swift, we can have: func someFunction(someParameter: someType, someOtherParameter: someOtherType) We call it like so: someFunction(x, y) We also have this: func someFunction(someParameter: someType, someOtherParameter: someOtherType?) We may call this like so: someFunc(x, y) OR someFunc(x, nil) However

Use function returning incomplete type as default argument

空扰寡人 提交于 2019-12-07 13:17:16
问题 When I try to compile and run this code (only the first three lines really matter): class object; object getObject(); void doSomething(object o = getObject()); class object{ public: int num = 0; }; object getObject(){ return {}; } void doSomething(object o){ o.num = 5; } int main(){} I get this error: main.cpp:3:39: error: invalid use of incomplete type 'class object' void doSomething(object o = getObject()); ^ main.cpp:1:7: note: forward declaration of 'class object' class object; ^ How

Elixir default parameters for named functions with multiple clauses

此生再无相见时 提交于 2019-12-07 10:58:33
问题 I have trouble understanding how default parameters interact with multiple clauses in named functions. It boils down to, why does the following snippet work? defmodule Lists do def sum([], total \\ 0), do: total def sum([h|t], total), do: h + sum(t, total) end From my understanding this gets expanded by the compiler into: defmodule Lists do def sum([]), do: sum([], 0) def sum([], total), do: total def sum([h|t], total), do: h + sum(t, total) end So I would expect the following to happen: iex

Dereferencing a function with default arguments - C++14 vs C++11

大城市里の小女人 提交于 2019-12-06 18:33:24
问题 Following code can't be compiled with g++ version 5.4.0 with option -std=c++1y : void f(int=0) ; int main() { f(); // ok (*f)(2);// ok (*f)();// ok c++11; error with c++14: too few arguments to function return 0; } The function declared to have default argument, so what is wrong here? thanks for help. And why does g++ -c -std=c++11 compile? 回答1: Accepting (*f)() as valid is a GCC bug. The letter of the standard indicates that using a function name with unary * should cause the function name