default-arguments

In C++, is a constructor with only default arguments a default constructor?

偶尔善良 提交于 2019-11-28 10:59:37
In the following code: struct Foo { Foo(int x=0); }; Does the constructor count as a default constructor? C++98 §12.1/5 (emphasis mine) : A default constructor for a class X is a constructor of X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared. So yes, it does count as a default constructor. See also . 来源: https://stackoverflow.com/questions/11250690/in-c-is-a-constructor-with-only-default-arguments-a-default-constructor

C++ Default Argument Error

蓝咒 提交于 2019-11-28 07:39:10
问题 Any Idea why this error is coming up at compile time? ComplexNumber.cpp:21: error: default argument given for parameter 1 of ‘void ComplexNumber::print(std::ostream&) const’ ComplexNumber.h:17: error: after previous specification in ‘void ComplexNumber::print(std::ostream&) const’ Here is my code at those certain areas: ComplexNumber.cpp 21 void ComplexNumber::print(ostream & out = cout) const { ComplexNumber.h 17 void print(ostream & out = cout) const; 回答1: You should only specify the

function overloading vs default argument in c++

独自空忆成欢 提交于 2019-11-28 02:09:24
问题 Hi i have a confusion or to say more i need to understand something. I have a procedure and another overloaded procedure of same. string conct (string a, string b) { string str = conct(a, b, "string"); return str; } string conct (string a, string b, const char* c) { // do the processing; return concatenated_string; } is it possible that instead of having two overloaded functions, if i make c in the overloaded function as default argument. So that even if someone passes only two arguments, i

How can I cleanly specify which arguments I am passing and which remain default?

丶灬走出姿态 提交于 2019-11-28 01:04:59
Asked because of this : Default argument in c++ Say I have a function such as this: void f(int p1=1, int p2=2, int p3=3, int p4=4); And I want to call it using only some of the arguments - the rest will be the defaults. Something like this would work: template<bool P1=true, bool P2=true, bool P3=true, bool P4=true> void f(int p1=1, int p2=2, int p3=3, int p4=4); // specialize: template<> void f<false, true, false, false>(int p1) { f(1, p1); } template<> void f<false, true, true, false>(int p1, int p2) { f(1, p1, p2); } // ... and so on. // Would need a specialization for each combination of

Default argument vs overloads in C++

爱⌒轻易说出口 提交于 2019-11-27 22:09:37
问题 For example, instead of void shared_ptr::reset() noexcept; template <typename Y> void shared_ptr::reset(Y* ptr); one may think of template <typename Y = T> void shared_ptr::reset(Y* ptr = nullptr); I think performance difference is negligible here, and the second version is more concise. Is there any specific reason the C++ standard goes the first way? The same question has been asked for the Kotlin language, and default argument is preferred there. Update: std::unique_ptr::reset() follows

error: default argument given for parameter 1

*爱你&永不变心* 提交于 2019-11-27 20:01:25
问题 I'm getting this error message with the code below: class Money { public: Money(float amount, int moneyType); string asString(bool shortVersion=true); private: float amount; int moneyType; }; First I thought that default parameters are not allowed as a first parameter in C++ but it is allowed. 回答1: You are probably redefining the default parameter in the implementation of the function. It should only be defined in the function declaration. //bad (this won't compile) string Money::asString

How to use source_location in a variadic template function?

我怕爱的太早我们不能终老 提交于 2019-11-27 10:44:57
问题 The C++20 feature std::source_location is used to capture information about the context in which a function is called. When I try to use it with a variadic template function, I encountered a problem: I can't see a place to put the source_location parameter. The following doesn't work because variadic parameters have to be at the end: // doesn't work template <typename... Args> void debug(Args&&... args, const std::source_location& loc = std::source_location::current()); The following doesn't

Using default value when calling a function

一曲冷凌霜 提交于 2019-11-27 08:05:55
In PHP you can call a function. by not calling all parameters by using these statement. function test($t1 ='test1',$t2 ='test2',$t3 ='test3') { echo "$t1, $t2, $t3"; } And you can just use the function like this test(); So lets just say I want the last one to be different but not the others. The only way I can thing of is by doing this with no success: test('test1','test2','hi i am different'); I tried this: test(,,'hi i am different'); test(default,default,'hi i am different'); What is the best way to do something like this? Use arrays : function test($options = array()) { $defaults = array(

How can I cleanly specify which arguments I am passing and which remain default?

扶醉桌前 提交于 2019-11-27 04:46:02
问题 Asked because of this : Default argument in c++ Say I have a function such as this: void f(int p1=1, int p2=2, int p3=3, int p4=4); And I want to call it using only some of the arguments - the rest will be the defaults. Something like this would work: template<bool P1=true, bool P2=true, bool P3=true, bool P4=true> void f(int p1=1, int p2=2, int p3=3, int p4=4); // specialize: template<> void f<false, true, false, false>(int p1) { f(1, p1); } template<> void f<false, true, true, false>(int p1

In C++, is a constructor with only default arguments a default constructor?

断了今生、忘了曾经 提交于 2019-11-27 03:56:50
问题 In the following code: struct Foo { Foo(int x=0); }; Does the constructor count as a default constructor? 回答1: C++98 §12.1/5 (emphasis mine) : A default constructor for a class X is a constructor of X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared. So yes, it does count as a default constructor. See also. 来源: https://stackoverflow.com/questions/11250690/in-c-is-a-constructor-with-only-default-arguments