www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Overloading const member function

reply Nub Public <nubpublic gmail.com> writes:
Why doesn't overloading of const member functions work?

struct S {
	int a, b;
	
	int A() const { return a; }
	ref int A() { return a; }
	
	int min() const { return A() < b ? A() : b; }
}

DMD2 compiler error: S.A () is not callable using argument types () const

Is it a bug or by design?
Jun 23 2011
next sibling parent reply Daniel Gibson <metalcaedes gmail.com> writes:
Am 23.06.2011 10:11, schrieb Nub Public:
 Why doesn't overloading of const member functions work?
 
 struct S {
     int a, b;
     
     int A() const { return a; }
     ref int A() { return a; }
     
     int min() const { return A() < b ? A() : b; }
 }
 
 DMD2 compiler error: S.A () is not callable using argument types () const
 
 Is it a bug or by design?
how do you want to call the different A() functions? i.e. how will D know which one you want to call?
Jun 23 2011
next sibling parent Nub Public <nubpublic gmail.com> writes:
On 6/23/2011 4:14 PM, Daniel Gibson wrote:
 Am 23.06.2011 10:11, schrieb Nub Public:
 Why doesn't overloading of const member functions work?

 struct S {
      int a, b;

      int A() const { return a; }
      ref int A() { return a; }

      int min() const { return A()<  b ? A() : b; }
 }

 DMD2 compiler error: S.A () is not callable using argument types () const

 Is it a bug or by design?
how do you want to call the different A() functions? i.e. how will D know which one you want to call?
It would depend on whether the instance of the struct is mutable or not. eg. the min() function is const, so the this object is const, and it would resolve to calling A() const.
Jun 23 2011
prev sibling parent kenji hara <k.hara.pg gmail.com> writes:
2011/6/23 Daniel Gibson <metalcaedes gmail.com>:
 how do you want to call the different A() functions? i.e. how will D
 know which one you want to call?
In member function S.min, member function A is called from implicit 'this' variable typed const(S). So
 =A0 =A0 int min() const { return A() < b ? A() : b; }
is semantically analyzed to int min() const { return this.A() < this.b ? this.A() : this.b; } , then this.A() should be resolved overloads as int A() const. Kenji
Jun 23 2011
prev sibling next sibling parent reply kenji hara <k.hara.pg gmail.com> writes:
This is definitely bug of current dmd.
It is already filed in bugzilla, see
http://d.puremagic.com/issues/show_bug.cgi?id=3D1983 .

Kenji

2011/6/23 Nub Public <nubpublic gmail.com>:
 Why doesn't overloading of const member functions work?

 struct S {
 =A0 =A0 =A0 =A0int a, b;

 =A0 =A0 =A0 =A0int A() const { return a; }
 =A0 =A0 =A0 =A0ref int A() { return a; }

 =A0 =A0 =A0 =A0int min() const { return A() < b ? A() : b; }
 }

 DMD2 compiler error: S.A () is not callable using argument types () const

 Is it a bug or by design?
Jun 23 2011
parent Nub Public <nubpublic gmail.com> writes:
I see. Thanks.

On 6/23/2011 4:19 PM, kenji hara wrote:
 This is definitely bug of current dmd.
 It is already filed in bugzilla, see
 http://d.puremagic.com/issues/show_bug.cgi?id=1983 .

 Kenji

 2011/6/23 Nub Public<nubpublic gmail.com>:
 Why doesn't overloading of const member functions work?

 struct S {
         int a, b;

         int A() const { return a; }
         ref int A() { return a; }

         int min() const { return A()<  b ? A() : b; }
 }

 DMD2 compiler error: S.A () is not callable using argument types () const

 Is it a bug or by design?
Jun 23 2011
prev sibling parent Trass3r <un known.com> writes:
Am 23.06.2011, 10:11 Uhr, schrieb Nub Public <nubpublic gmail.com>:

 Why doesn't overloading of const member functions work?

 struct S {
 	int a, b;
 	
 	int A() const { return a; }
 	ref int A() { return a; }
 	
 	int min() const { return A() < b ? A() : b; }
 }

 DMD2 compiler error: S.A () is not callable using argument types () const

 Is it a bug or by design?
1) This particular case is probably a bug. 2) Your code is typical C++ style. In D you would normally use 'void A(int x) { a = x; }' to define a setter. 3) Structs in D are POD structs like in C. Thus all members are public by default and setters/getters are unusual. If you really need to restrict access via a getter (x)or setter you have to declare a and b private.
Jun 23 2011