default-arguments

Why can't the compiler deduce the template type from default arguments?

懵懂的女人 提交于 2019-11-26 22:39:54
I was surprised the following code resulted in a could not deduce template argument for T error: struct foo { template <typename T> void bar(int a, T b = 0.0f) { } }; int main() { foo a; a.bar(5); return 0; } Calling a.bar<float>(5) fixes the issue. Why can't the compiler deduce the type from the default argument? In C++03, the specification explicitly prohibits the default argument from being used to deduce a template argument (C++03 §14.8.2/17): A template type-parameter cannot be deduced from the type of a function default argument. In C++11, you can provide a default template argument for

Where to put default parameter value in C++? [duplicate]

隐身守侯 提交于 2019-11-26 15:39:15
This question already has an answer here: Default value of function parameter 4 answers What's the place for the default parameter value? Just in function definition, or declaration, or both places? Default parameter values must appear on the declaration, since that is the only thing that the caller sees. EDIT: As others point out, you can have the argument on the definition, but I would advise writing all code as if that wasn't true. sharptooth You can do either, but never both. Usually you do it at function declaration and then all callers can use that default value. However you can do that

Using default value when calling a function

守給你的承諾、 提交于 2019-11-26 13:59:19
问题 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

Default argument in the middle of parameter list?

白昼怎懂夜的黑 提交于 2019-11-26 09:24:37
问题 I saw a function declaration in our code that looked as follows void error(char const *msg, bool showKind = true, bool exit); I thought first that this is an error because you cannot have default arguments in the middle of functions, but the compiler accepted this declaration. Has anyone seen this before? I\'m using GCC4.5. Is this a GCC extension? The weird thing is, if I take this out in a separate file and try to compile, GCC rejects it. I\'ve double checked everything, including the

How to pass a default argument value of an instance member to a method?

旧巷老猫 提交于 2019-11-26 08:26:18
I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the following error message: NameError: name 'self' is not defined I want the method to behave like this: C("abc").process() # prints "abc" C("abc").process("xyz") # prints "xyz" What is the problem here, why does this not work? And how could I make this work? Adam Wagner You can't really define this as the default value, since the default value is

Why can&#39;t the compiler deduce the template type from default arguments?

一曲冷凌霜 提交于 2019-11-26 08:26:13
问题 I was surprised the following code resulted in a could not deduce template argument for T error: struct foo { template <typename T> void bar(int a, T b = 0.0f) { } }; int main() { foo a; a.bar(5); return 0; } Calling a.bar<float>(5) fixes the issue. Why can\'t the compiler deduce the type from the default argument? 回答1: In C++03, the specification explicitly prohibits the default argument from being used to deduce a template argument (C++03 §14.8.2/17): A template type-parameter cannot be

Where to put default parameter value in C++? [duplicate]

断了今生、忘了曾经 提交于 2019-11-26 04:32:42
问题 This question already has an answer here: Default value of function parameter 4 answers What\'s the place for the default parameter value? Just in function definition, or declaration, or both places? 回答1: Default parameter values must appear on the declaration, since that is the only thing that the caller sees. EDIT: As others point out, you can have the argument on the definition, but I would advise writing all code as if that wasn't true. 回答2: You can do either, but never both. Usually you