default-parameters

Use function returning incomplete type as default argument

♀尐吖头ヾ 提交于 2019-12-05 19:24:14
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 would I get it to compile without changing the order of everything? In my actual code, the declarations are

Can an unnamed parameter of function have a default value?

三世轮回 提交于 2019-12-05 18:16:17
问题 Is the following code legal in C++? void f(void* = 0) {} int main() { f(); } Which page of the C++ standard states that this usage is legal? 回答1: Yes, it's legal. There is no standard wording to allow this combination of features specifically; there simply isn't any to disallow it, either. Default argument syntax applies to function parameters in a parameter-declaration : [C++11: 8.3.6/1]: If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a

Elixir default parameters for named functions with multiple clauses

拜拜、爱过 提交于 2019-12-05 14:14:51
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(1)> Lists.sum [1,2,3,4] ** (FunctionClauseError) no function clause matching in Lists.sum/1 instead it

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

岁酱吖の 提交于 2019-12-04 22:30:43
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? 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 to decay into a pointer. The pointer should then be dereferenced to obtain the functions address for the call

Is there a way to use the default value on a non-optional parameter when null is passed?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 15:09:10
问题 For example, if I have the following data class: data class Data( val name: String = "", val number: Long = 0 ) And functions that can return null : fun newName(): String? {} fun newNumber(): Long? {} I know I can use the following to use the value of the functions if they are not null : val newName = newName() val newNumber = newNumber() val data = Data( if (newName != null) newName else "", if (newNumber != null) newNumber else 0 ) But is there a way to just use the default value specified

Why is this default template parameter not allowed?

谁说我不能喝 提交于 2019-12-04 05:16:28
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_; }; And attempt to instantiate an automatic variable like this: AlignedMemory blah(512, 512); This

Documenting with Sphinx python methods that do have default parameters with sentinel objects?

孤人 提交于 2019-12-04 04:40:14
问题 If you want to be able to allow people to call some methods using None you have to do use a sentinel object when you define the method. _sentinel = object() def foo(param1=_sentinel): ... This would allow you to call foo(param1=None) and be able to make the difference between a call like foo() . The problem is that when Sphinx does document the method it will write something like mymodule.foo(param1=<object object at 0x108c1a520>) How can I convince Sphinx to have a user friendly output for

How can I use a static method as a default parameter for the strategy design pattern?

匆匆过客 提交于 2019-12-04 03:24:31
问题 I want to make a class that uses a strategy design pattern similar to this: class C: @staticmethod def default_concrete_strategy(): print("default") @staticmethod def other_concrete_strategy(): print("other") def __init__(self, strategy=C.default_concrete_strategy): self.strategy = strategy def execute(self): self.strategy() This gives the error: NameError: name 'C' is not defined Replacing strategy=C.default_concrete_strategy with strategy=default_concrete_strategy will work but, left as

PhpStorm - JavaScript function parameter IDE error

醉酒当歌 提交于 2019-12-04 03:05:22
问题 I am trying to build a reusable JavaScript function that makes use of default parameters. However, the IDE warns me I do something wrong. The code is this: function standardAjaxRequest(process_script_path, redirect = false) { var loading = $(".loading"); var response_message = $(".response_message"); // runs the ajax to add the entry submitted form.on("submit", function(event) { event.preventDefault(); removeNoticeError(); var data = form.serialize(); $.ajax({ url: process_script_path, type:

javaScript function - why my default argument fails?

我是研究僧i 提交于 2019-12-04 02:34:02
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. Default parameters is assigned only if no value or undefined is passed let defaultStyle = { one: 1, two: 2, three: 3 } function styling(style = defaultStyle, ...ruleSetStock) {