default-parameters

C++14 lambda's default argument type deduction depending on preceding arguments

两盒软妹~` 提交于 2019-12-23 19:05:24
问题 Is this not valid as C++14? auto f = [](auto x, auto y = std::decay_t<decltype(x)>{}) { }; f(0); I was expecting it to be roughly equivalent to auto f = [](int x, int y) { }; f(0, int{}); Neither GCC 6.3 nor Clang 4.0 accepted my code. http://ideone.com/b7b4SK GCC http://ideone.com/EyLYaL Clang Is it related to my lack of understanding of C++ template deduction phases? Does the 1400 pages long spec actually has an explicit answer to my question? Update To summarize, my problem can in fact be

Virtual function default parameters and overloading

送分小仙女□ 提交于 2019-12-22 10:09:08
问题 This question refers to common problems discussed in these questions: Can virtual functions have default parameters? Virtual functions default parameters Here is what currently happens in c++ with default parameters to virtual functions: struct Base { virtual void foo(int one = 1, int two = 2) { cout << "one: " << one << " two: " << two << endl; } }; struct Derived : public Base { virtual void foo(int one = 3, int two = 4) { Base::foo(one, two); cout << " derived!" << endl; } }; int main() {

function call with default parameter

纵然是瞬间 提交于 2019-12-20 01:12:05
问题 I wrote an examination about C++ programming. There was one question where I and my professor didn't agree. The question was, does the following function work or not: #include <iostream> using namespace std; void f(int=4, long=10, double=3.14); int main( int argc , char ** argv ) { f( , ,8); return EXIT_SUCCESS; } void f(int i, long l, double d) { cout << i << " " << " " << l << " " << d; } I said it would not work, but my professor said it will definitely work because of the default

Why can member variables not be used as defaults for parameters? [duplicate]

。_饼干妹妹 提交于 2019-12-19 07:09:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Nonstatic member as a default argument of a nonstatic member function Correct me if I am wrong, but the way I think default parameters work is this: When the compiler sees the function call, it starts pushing the parameters onto the stack. When it runs out of parameters, it will start pushing the defaults onto the stack until all required parameters are filled (I know this is a simplification, since parameters

Function pointers with default parameters in C++

纵饮孤独 提交于 2019-12-18 18:37:15
问题 How does C++ handle function pointers in relation to functions with defaulted parameters? If I have: void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float = 10.0f); Which function pointers can I use in relation to which function? 回答1: Both foo() and bar() can only be assigned to func_ptr2 . §8.3.6/2 : A default argument is not part of the type of a function. [Example: int f(int = 0); void h() { int j = f

C#, default parameter value for an IntPtr

南笙酒味 提交于 2019-12-17 16:56:10
问题 I'd like to use a default parameter value of IntPtr.Zero in a function that takes an IntPtr as an argument. This is not possible as IntPtr.Zero is not a compile time constant. Is there any way I can do what I want? 回答1: Somewhat unintuitive, to put it mildly, you get it by using the new operator: void Foo(IntPtr arg = new IntPtr()) { } That was for fun, you probably enjoy this one better: void Foo(IntPtr arg = default(IntPtr)) { } 回答2: Since IntPtr is a struct, you could use Nullable-of-T?

T-SQL - function with default parameters

☆樱花仙子☆ 提交于 2019-12-17 04:52:10
问题 I have this script: CREATE FUNCTION dbo.CheckIfSFExists(@param1 INT, @param2 BIT = 1 ) RETURNS BIT AS BEGIN IF EXISTS ( bla bla bla ) RETURN 1; RETURN 0; END GO I want to use it in a procedure in this way: IF dbo.CheckIfSFExists( 23 ) = 0 SET @retValue = 'bla bla bla'; But I get the error: An insufficient number of arguments were supplied for the procedure or function dbo.CheckIfSFExists. Why does it not work? 回答1: you have to call it like this SELECT dbo.CheckIfSFExists(23, default) From

“Least Astonishment” and the Mutable Default Argument

一曲冷凌霜 提交于 2019-12-14 03:58:54
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it "a dramatic design flaw"

“Least Astonishment” and the Mutable Default Argument

﹥>﹥吖頭↗ 提交于 2019-12-13 07:14:45
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it "a dramatic design flaw"

Default parameter value for a TSomething in Delphi

a 夏天 提交于 2019-12-12 11:40:56
问题 I'd like to know if this is possible in Delphi (or if there's a clean way around it): type TSomething = record X, Y : Integer; end; GetSomething( x, y ) -> Returns record with those values. ... and then you have this function with TSomething as parameter, and you want to default it as function Foo( Something : TSomething = GetSomething( 1, 3 ); The compiler spits an error here, however I'm not sure if there's a way around it! Can this be done? 回答1: The easiest way is to use overloaded