www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const overload

reply so <so so.so> writes:
------------JLj78VjCjtPYdZL4UEF3AL
Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes
Content-Transfer-Encoding: 7bit

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++?
------------JLj78VjCjtPYdZL4UEF3AL
Content-Disposition: attachment; filename=test2.d
Content-Type: application/octet-stream; name=test2.d
Content-Transfer-Encoding: Base64

aW1wb3J0IHN0ZC5zdGRpbzsKCnN0cnVjdCBTIHsKCglyZWYgUyBmdW4oKSB7CgkJ
d3JpdGVsbigiZnVuIik7CgkJcmV0dXJuIHRoaXM7Cgl9CgoJUyBmdW4oKSBjb25z
dCB7CgkJd3JpdGVsbigiZnVuIGNvbnN0Iik7CgkJcmV0dXJuIFMoKTsKCX0KfQoK
dm9pZCBtYWluKCkgewoKCVMgYSA9IFMoKTsKCWEuZnVuKCkgPSBhLmZ1bigpOwp9
Cg==

------------JLj78VjCjtPYdZL4UEF3AL--
Sep 23 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Sep 23 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Sep 23 2011
prev sibling next sibling parent so <so so.so> writes:
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++.
Sep 23 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Sep 23 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Sep 23 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Sep 23 2011
prev sibling next sibling parent so <so so.so> writes:
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.
Sep 23 2011
prev sibling parent so <so so.so> writes:
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!
Sep 23 2011