Re: Const vs Non const method

2016-03-07 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 7 March 2016 at 18:44:01 UTC, Namespace wrote: Honestly speaking, I think this case is impossible to solve in C++. I'll show my fellow students the advantages of D over C++ in next couple of weeks, and this example is pretty good. :) :-) Good luck!

Re: Const vs Non const method

2016-03-07 Thread Namespace via Digitalmars-d-learn
On Monday, 7 March 2016 at 18:17:18 UTC, Ola Fosheim Grøstad wrote: On Monday, 7 March 2016 at 16:30:48 UTC, Namespace wrote: Thanks to the wildcard modifier inout. Is there any possible way to do the same in C++? In this specific case you could do it with a macro if you don't mind dirty

Re: Const vs Non const method

2016-03-07 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 7 March 2016 at 16:30:48 UTC, Namespace wrote: Thanks to the wildcard modifier inout. Is there any possible way to do the same in C++? In this specific case you could do it with a macro if you don't mind dirty macros, but you really should implement the const version explicitly or

Re: Const vs Non const method

2016-03-07 Thread Namespace via Digitalmars-d-learn
Let's use an example: import std.stdio; class Visitor { public: void visit(inout A) { writeln("visit A"); } void visit(inout B) { writeln("visit B"); } } class A { public: void accept(Visitor v) inout { v.visit(this); } } class B : A {

Re: Const vs Non const method

2016-03-07 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 7 March 2016 at 10:52:53 UTC, Ola Fosheim Grøstad wrote: On Sunday, 6 March 2016 at 17:53:47 UTC, Namespace wrote: What would be the C++ way? Is there any comfortable way to solve this problem in a nice way like D? C++ has a non-idiomatic language culture. There are many ways to

Re: Const vs Non const method

2016-03-07 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Sunday, 6 March 2016 at 17:53:47 UTC, Namespace wrote: What would be the C++ way? Is there any comfortable way to solve this problem in a nice way like D? C++ has a non-idiomatic language culture. There are many ways to do it. One clean way could be to use a templated method, another way

Re: Const vs Non const method

2016-03-06 Thread Namespace via Digitalmars-d-learn
On Thursday, 25 February 2016 at 10:59:43 UTC, Rene Zwanenburg wrote: On Thursday, 25 February 2016 at 10:44:49 UTC, Andrea Fontana wrote: Check this simple code: http://dpaste.dzfl.pl/2772c9144f1c I can't understand how to minimize code duplication for function like get(). Of course on real

Re: Const vs Non const method

2016-02-25 Thread Rene Zwanenburg via Digitalmars-d-learn
On Thursday, 25 February 2016 at 10:44:49 UTC, Andrea Fontana wrote: Check this simple code: http://dpaste.dzfl.pl/2772c9144f1c I can't understand how to minimize code duplication for function like get(). Of course on real case body is much bigger and complex than that. The only way I found

Re: Const vs Non const method

2016-02-25 Thread Andrea Fontana via Digitalmars-d-learn
On Thursday, 25 February 2016 at 10:48:34 UTC, Namespace wrote: Try inout: import std.stdio; struct Inner { int field = 3; } struct Test { auto get() inout { return inner; } private Inner inner; } void main() { {

Re: Const vs Non const method

2016-02-25 Thread Namespace via Digitalmars-d-learn
Try inout: import std.stdio; struct Inner { int field = 3; } struct Test { auto get() inout { return inner; } private Inner inner; } void main() { { Test test; test.get.field = 4; }

Const vs Non const method

2016-02-25 Thread Andrea Fontana via Digitalmars-d-learn
Check this simple code: http://dpaste.dzfl.pl/2772c9144f1c I can't understand how to minimize code duplication for function like get(). Of course on real case body is much bigger and complex than that. The only way I found is to move the body of function inside a mixin template: mixin