Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
September 23, 2011 const overload | ||||
---|---|---|---|---|
| ||||
Attachments: | Hello everyone. I asked this a few times with no response. Could anyone explain me what is the rational behind this? Why it won't distinguish mutable overload from immutable as in C++? |
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | On Friday, September 23, 2011 23:19:15 so wrote:
> Hello everyone.
>
> I asked this a few times with no response.
> Could anyone explain me what is the rational behind this?
> Why it won't distinguish mutable overload from immutable as in C++?
That compiles fine with the lastest dmd from git. Is it not compiling with the latest release (dmd 2.055)?
- Jonathan M Davis
|
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | On Fri, 23 Sep 2011 16:19:15 -0400, so <so@so.so> wrote:
> Hello everyone.
>
> I asked this a few times with no response.
> Could anyone explain me what is the rational behind this?
> Why it won't distinguish mutable overload from immutable as in C++?
example? I'm afraid I don't really understand the question. overloads based on const/immutable are supported:
struct S
{
void foo() { writeln("mutable");}
void foo() const { writeln("const");}
void foo() immutable { writeln("immutable");}
}
void main()
{
S s;
immutable(S) s2;
const(S) s3;
s.foo();
s2.foo();
s3.foo();
}
outputs:
mutable
immutable
const
-Steve
|
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Fri, 23 Sep 2011 23:27:02 +0300, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> On Friday, September 23, 2011 23:19:15 so wrote:
>> Hello everyone.
>>
>> I asked this a few times with no response.
>> Could anyone explain me what is the rational behind this?
>> Why it won't distinguish mutable overload from immutable as in C++?
>
> That compiles fine with the lastest dmd from git. Is it not compiling with the
> latest release (dmd 2.055)?
>
> - Jonathan M Davis
It compiles fine but the result troubling, wouldn't you expect:
fun
fun const
as a result? This is how it works in C++.
|
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Fri, 23 Sep 2011 16:27:23 -0400, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> On Fri, 23 Sep 2011 16:19:15 -0400, so <so@so.so> wrote:
>
>> Hello everyone.
>>
>> I asked this a few times with no response.
>> Could anyone explain me what is the rational behind this?
>> Why it won't distinguish mutable overload from immutable as in C++?
>
> example? I'm afraid I don't really understand the question.
Sorry, didn't notice the attachment...
re-examining...
-Steve
|
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | On Fri, 23 Sep 2011 16:35:59 -0400, so <so@so.so> wrote: > On Fri, 23 Sep 2011 23:27:02 +0300, Jonathan M Davis <jmdavisProg@gmx.com> wrote: > >> On Friday, September 23, 2011 23:19:15 so wrote: >>> Hello everyone. >>> >>> I asked this a few times with no response. >>> Could anyone explain me what is the rational behind this? >>> Why it won't distinguish mutable overload from immutable as in C++? >> >> That compiles fine with the lastest dmd from git. Is it not compiling with the >> latest release (dmd 2.055)? >> >> - Jonathan M Davis > > It compiles fine but the result troubling, wouldn't you expect: > > fun > fun const > > as a result? This is how it works in C++. steves@steve-laptop:~/testd$ cat testconst.cpp #include <iostream> using namespace std; struct S { S& fun() { cout << "fun" << endl; return *this; } S fun() const { cout << "fun const" << endl; return S(); } }; int main() { S a; a.fun() = a.fun(); return 0; } steves@steve-laptop:~/testd$ g++ -o testconst testconst.cpp steves@steve-laptop:~/testd$ ./testconst fun fun steves@steve-laptop:~/testd$ Seems, um to be the same, no? -Steve |
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | On Friday, September 23, 2011 23:35:59 so wrote:
> On Fri, 23 Sep 2011 23:27:02 +0300, Jonathan M Davis <jmdavisProg@gmx.com>
>
> wrote:
> > On Friday, September 23, 2011 23:19:15 so wrote:
> >> Hello everyone.
> >>
> >> I asked this a few times with no response.
> >> Could anyone explain me what is the rational behind this?
> >> Why it won't distinguish mutable overload from immutable as in C++?
> >
> > That compiles fine with the lastest dmd from git. Is it not compiling
> > with the
> > latest release (dmd 2.055)?
> >
> > - Jonathan M Davis
>
> It compiles fine but the result troubling, wouldn't you expect:
>
> fun
> fun const
>
> as a result? This is how it works in C++.
It uses the const version if the struct or class is const. And in neither case in your program is it const. It's mutable in both, so the mutable overload is the one that gets called in both places. Why would the const version get called? How would it know to call that one instead of the mutable one? I don't know how it could work any other way. I would have thought that it would be exactly the same in C++, but I don't overload on constness very often in either language, so I'm not necessarily familiar with all of the ins and outs of why C++ picks const over mutable in such cases.
- Jonathan M Davis
|
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Fri, 23 Sep 2011 23:44:52 +0300, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> steves@steve-laptop:~/testd$ cat testconst.cpp
> #include <iostream>
> using namespace std;
>
> struct S {
>
> S& fun() {
> cout << "fun" << endl;
> return *this;
> }
>
> S fun() const {
> cout << "fun const" << endl;
> return S();
> }
> };
>
> int main() {
>
> S a;
> a.fun() = a.fun();
> return 0;
> }
> steves@steve-laptop:~/testd$ g++ -o testconst testconst.cpp
> steves@steve-laptop:~/testd$ ./testconst
> fun
> fun
> steves@steve-laptop:~/testd$
>
>
> Seems, um to be the same, no?
>
> -Steve
You are right, i am confused. Sorry about that.
|
September 23, 2011 Re: const overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Fri, 23 Sep 2011 23:44:41 +0300, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> It uses the const version if the struct or class is const. And in neither case
> in your program is it const. It's mutable in both, so the mutable overload is
> the one that gets called in both places. Why would the const version get
> called? How would it know to call that one instead of the mutable one? I don't
> know how it could work any other way. I would have thought that it would be
> exactly the same in C++, but I don't overload on constness very often in
> either language, so I'm not necessarily familiar with all of the ins and outs
> of why C++ picks const over mutable in such cases.
>
> - Jonathan M Davis
I was just testing you!
|
Copyright © 1999-2021 by the D Language Foundation