default-parameters

Why can't I have template and default arguments?

ぐ巨炮叔叔 提交于 2019-11-29 01:39:02
问题 I changed a paremeter in a function to accept any kind of object using a template but I can't use it in conjunction with other default parameters, is there something I am missing? #include <string> #include <iostream> class MyClass { public: std::wstring msg = L"hey"; MyClass(){}; }; class MyClass2{ public: template<class T> MyClass2(T* t, int i); }; template<class T> MyClass2::MyClass2(T* t,int i=0){ std::wcout << t->msg << std::endl; } int main(int argc, char **argv) { MyClass mc; MyClass2

How does `this` work in default parameters?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 00:51:56
问题 So... ES6¹ (which happens to be standardized a few hours ago) brings default parameters for functions similar to those in PHP, Python etc. I can do stuff like: function foo (bar = 'dum') { return bar; } foo(1); // 1 foo(); // 'dum' foo(undefined); // 'dum' MDN says that the default value for the parameter is evaluated at call time. Which means each time I call the function, the expression 'dum' is evaluated again (unless the implementation does some weird optimizations which we don't care

Platform independent /dev/null in c++ [duplicate]

我是研究僧i 提交于 2019-11-28 23:08:29
Possible Duplicate: Implementing a no-op std::ostream Is there any stream equivalent of NULL in c++? I want to write a function that takes in a stream if the user wants to have the internal outputted to somewhere, but if not, the output goes into some fake place void data(std::stream & stream = fake_stream){ stream << "DATA" ; } i want to be able to chose to do data() or data(std::cout) Edit : Taken from @Johannes Schaub - litb's mail here with slight modifications: template<typename Ch, typename Traits = std::char_traits<Ch> > struct basic_nullbuf : std::basic_streambuf<Ch, Traits> { typedef

C#, default parameter value for an IntPtr

旧街凉风 提交于 2019-11-28 01:49:08
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? 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)) { } Since IntPtr is a struct, you could use Nullable-of-T? static void SomeMethod(IntPtr? ptr = null) { var actualPtr = ptr ?? IntPtr.Zero; //... } 来源: https://stackoverflow

Default template parameter partial specialization

限于喜欢 提交于 2019-11-27 19:14:58
Please explain to me why the following piece of code complies and works perfectly. I am very confused. #include<iostream> template<class A = int, class B=double> class Base {}; template<class B> class Base <int, B> { public: Base() { std::cout<<"it works!!!!!\n"; } }; int main() { Base<> base; // it prints "it works!!!!!" return 0; } Shouldn't it fall into the generalized form of the template class Base? The default argument applies to the specialization -- and, in fact, a specialization must accept (so to speak) the base template's default argument(s). Attempting to specify a default in the

Default Values for function parameters in Python [duplicate]

偶尔善良 提交于 2019-11-27 17:57:16
Possible Duplicate: default value of parameter as result of instance method While it is possible to set default values to function parameters in python: def my_function(param_one='default') ... It seems not to be possible to access the current instance (self): class MyClass(..): def my_function(self, param_one=self.one_of_the_vars): ... My question(s): Is this true that I cannot access the current instance to set the default parameter in functions? If it is not possble: what are the reasons and is it imaginable that this will be possible in future versions of python? It's written as: def my

Platform independent /dev/null in c++ [duplicate]

痞子三分冷 提交于 2019-11-27 14:33:14
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Implementing a no-op std::ostream Is there any stream equivalent of NULL in c++? I want to write a function that takes in a stream if the user wants to have the internal outputted to somewhere, but if not, the output goes into some fake place void data(std::stream & stream = fake_stream){ stream << "DATA" ; } i want to be able to chose to do data() or data(std::cout) 回答1: Edit : Taken from @Johannes Schaub -

Type Hinting: Default Parameters

坚强是说给别人听的谎言 提交于 2019-11-27 13:25:45
PHP 5 Type Hinting PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects ( by specifying the name of the class in the function prototype ) or arrays ( since PHP 5.1 ). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call. The following excerpt from the above: if NULL is used as the default parameter value, it will be allowed as an argument for any later call. Does the above mean: if default parameters are to used use with type hinting, it can have only have NULL as the default value. i.e. the code in

Compiler error “Default parameter specifiers are not permitted”

纵然是瞬间 提交于 2019-11-27 06:45:10
问题 Below is my code. public class PItem { public String content; public int count; public int fee; public int amount; public string description; // Default values public PItem(String _content = "", int _count = 0, int _fee = 0, string _description = "", int _amount = 0) { content = _content; count = _count < 0 ? 0 : _count; fee = _fee; description = _description; amount = _amount < 0 ? 0 : _amount; } } This is inside in a class. When I try to run a program it gives this error: Default parameter

Type Hinting: Default Parameters

扶醉桌前 提交于 2019-11-27 04:04:35
问题 PHP 5 Type Hinting PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects ( by specifying the name of the class in the function prototype ) or arrays ( since PHP 5.1 ). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call. The following excerpt from the above: if NULL is used as the default parameter value, it will be allowed as an argument for any later call. Does the above mean: if default parameters