default-arguments

Default values for arguments [duplicate]

流过昼夜 提交于 2019-12-04 06:48:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function: def foo(L = []): L.append(1) print L Each time I call foo it will print a new list with more elements than previous time, e.g: >>> foo() [1] >>> foo() [1, 1] >>> foo() [1, 1, 1] Now consider the following function: def goo(a = 0): a += 1 print a When invoking it several times, we get the following picture: >>> goo() 1 >

Why does using this C++ function twice in one line cause a compile error?

不打扰是莪最后的温柔 提交于 2019-12-03 23:21:28
I ran into some trouble trying to implement a smart equality test macro-type template function in Visual C++ 2010 that had to do with a bug in VS in regard to default arguments of template functions . I fixed it by wrapping the value of the parameter in an extra function, but now I found that I can't use the function twice in one line! Header file: // example.h #pragma once #include <limits> namespace myspace { // Need to define this separately to avoid a Visual Studio bug template<typename T> T epsilon() { return std::numeric_limits<T>::epsilon(); } // A generic equality test template

Am I Allowed to Default a Template Argument in a Forward Declaration

我的未来我决定 提交于 2019-12-03 17:29:51
So I'm trying to understand what's happening with Boost's ptree implementation. In ptree.hpp basic_ptree is actually defined: template<class Key, class Data, class KeyCompare> class basic_ptree In ptree_fwd.hpp there is what looks like a forward declaration of basic_ptree but with a new template argument default: template < class Key, class Data, class KeyCompare = std::less<Key> > class basic_ptree; And finally in ptree_fwd.hpp ptree is typedef 'd: typedef basic_ptree<std::string, std::string> ptree; This is a forward declaration then in ptree_fwd.hpp, right? So I am allowed to default a

Unintuitive behaviour with struct initialization and default arguments

大憨熊 提交于 2019-12-03 11:01:35
public struct Test { public double Val; public Test(double val = double.NaN) { Val = val; } public bool IsValid { get { return !double.IsNaN(Val); } } } Test myTest = new Test(); bool valid = myTest.IsValid; The above gives valid==true because the constructor with default arg is NOT called and the object is created with the standard default val = 0.0. If the struct is a class the behaviour is valid==false which is what I would expect. I find this difference in behaviour and particularly the behaviour in the struct case suprising and unintuitive - what is going on? What does the default arg on

std::map argument with empty brace-initializers for default argument segfaults in GCC

谁都会走 提交于 2019-12-03 10:32:25
问题 Problem I got a bug report from user reporting a segfault in library I develop. The minimal example of the faulty code is: #include <map> #include <string> #include <iostream> void f(std::map<std::string, std::string> m = {}) { std::cout << m.size() << "\n"; for (const auto& s: m) { std::cout << s.first << "->" << s.second <<"\n"; } } int main() { f(); } When compiled with GCC (I tested 4.8.2 and 4.7.3) it correctly prints 0 as size of the container, but segfaults inside the loop (which

C++: Adding and redefinition of default arguments in real world

大城市里の小女人 提交于 2019-12-03 07:15:00
问题 There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example: void foo(int a, int b, int c = -1) { std::cout << "foo(" << a << ", " << b << ", " << c << ")\n"; } int main() { foo(1, 2); // output: foo(1, 2, -1) // void foo(int a, int b = 0, int c); // error: does not use default from surrounding scope void foo(int a, int b, int c = 30); foo(1, 2); // output: foo(1, 2, 30) // void foo(int a, int b, int c = 35); // error: we cannot redefine the

std::map argument with empty brace-initializers for default argument segfaults in GCC

一个人想着一个人 提交于 2019-12-03 01:04:12
Problem I got a bug report from user reporting a segfault in library I develop. The minimal example of the faulty code is: #include <map> #include <string> #include <iostream> void f(std::map<std::string, std::string> m = {}) { std::cout << m.size() << "\n"; for (const auto& s: m) { std::cout << s.first << "->" << s.second <<"\n"; } } int main() { f(); } When compiled with GCC (I tested 4.8.2 and 4.7.3) it correctly prints 0 as size of the container, but segfaults inside the loop (which shouldn't be executed at all). Workarounds However, I can fix the problem by changing the declaration to:

C++: Adding and redefinition of default arguments in real world

混江龙づ霸主 提交于 2019-12-02 20:47:37
There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example: void foo(int a, int b, int c = -1) { std::cout << "foo(" << a << ", " << b << ", " << c << ")\n"; } int main() { foo(1, 2); // output: foo(1, 2, -1) // void foo(int a, int b = 0, int c); // error: does not use default from surrounding scope void foo(int a, int b, int c = 30); foo(1, 2); // output: foo(1, 2, 30) // void foo(int a, int b, int c = 35); // error: we cannot redefine the argument in the same scope // has a default argument for c from a previous declaration void foo(int a, int

Missing default argument - compiler error

你说的曾经没有我的故事 提交于 2019-12-02 18:50:12
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 ? You can't have non-default parameters after your default parameters begin. Put another way, how would you specify a value for b leaving word to the default of "hello" ? The arguments with a default value

Default values for arguments [duplicate]

醉酒当歌 提交于 2019-12-02 10:48:59
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function: def foo(L = []): L.append(1) print L Each time I call foo it will print a new list with more elements than previous time, e.g: >>> foo() [1] >>> foo() [1, 1] >>> foo() [1, 1, 1] Now consider the following function: def goo(a = 0): a += 1 print a When invoking it several times, we get the following picture: >>> goo() 1 >>> goo() 1 >>> goo() 1 i.e. it does not print a larger value with every call. What is the reason behind such seemingly inconsistent behavior? Also, how is it