Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
October 22, 2015 What's wrong in this templatized operator overload ? | ||||
---|---|---|---|---|
| ||||
code: --- struct Foo { bool opIn_r(T)(T t){return false;} } static immutable Foo foo; // ouch //static Foo foo; // OK void main() { assert("a" !in foo); } --- output: --- Error: template Foo.opIn_r cannot deduce function from argument types !()(string) immutable, candidates are: runnable.Foo.opIn_r(T)(T t) --- |
October 22, 2015 Re: What's wrong in this templatized operator overload ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MobPassenger | On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote:
> code:
> ---
> struct Foo
> {
> bool opIn_r(T)(T t){return false;}
> }
>
This needs to be marked with const:
struct Foo
{
bool opIn_r(T)(T t) const {return false;}
}
|
October 22, 2015 Re: What's wrong in this templatized operator overload ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Thursday, 22 October 2015 at 04:01:16 UTC, Mike Parker wrote:
> On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote:
>> code:
>> ---
>> struct Foo
>> {
>> bool opIn_r(T)(T t){return false;}
>> }
>>
>
> This needs to be marked with const:
>
> struct Foo
> {
> bool opIn_r(T)(T t) const {return false;}
> }
what's the rationale ? what's guaranteed by the qualifier that's not already true without const ?
|
October 22, 2015 Re: What's wrong in this templatized operator overload ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MobPassenger | On Thursday, 22 October 2015 at 04:25:01 UTC, MobPassenger wrote:
> On Thursday, 22 October 2015 at 04:01:16 UTC, Mike Parker wrote:
>> On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote:
>>> code:
>>> ---
>>> struct Foo
>>> {
>>> bool opIn_r(T)(T t){return false;}
>>> }
>>>
>>
>> This needs to be marked with const:
>>
>> struct Foo
>> {
>> bool opIn_r(T)(T t) const {return false;}
>> }
>
> what's the rationale ? what's guaranteed by the qualifier that's not already true without const ?
`const` just means the function won't mutate the object. `const` functions can be safely called on mutable, const and immutable objects. Non-`const` functions can only be called on mutable objects.
|
October 22, 2015 Re: What's wrong in this templatized operator overload ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cauterite | On Thursday, 22 October 2015 at 05:17:29 UTC, Cauterite wrote:
> On Thursday, 22 October 2015 at 04:25:01 UTC, MobPassenger wrote:
>> On Thursday, 22 October 2015 at 04:01:16 UTC, Mike Parker wrote:
>>> On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote:
>>>> code:
>>>> ---
>>>> struct Foo
>>>> {
>>>> bool opIn_r(T)(T t){return false;}
>>>> }
>>>>
>>>
>>> This needs to be marked with const:
>>>
>>> struct Foo
>>> {
>>> bool opIn_r(T)(T t) const {return false;}
>>> }
>>
>> what's the rationale ? what's guaranteed by the qualifier that's not already true without const ?
>
> `const` just means the function won't mutate the object. `const` functions can be safely called on mutable, const and immutable objects. Non-`const` functions can only be called on mutable objects.
Thx for the explanations. By the way I knew that when const is applied to the return type it has to be surrounded between parens but so far I've never encountered the other case...And now I also remember why this attribte should rather be put at the right of the function declaration.
|
Copyright © 1999-2021 by the D Language Foundation