A few questions

Jacob Carlborg doob at me.com
Fri Jul 27 04:14:18 PDT 2012


On 2012-07-27 12:29, Namespace wrote:
> 1.
> Why are these two method header identitcal?
>
> const Foo some_function() {
>
> and
>
> Foo some_function() const {
>
> ?

The reason "const Foo some_function" is allowed is because in D this 
syntax is possible:

class Foo
{
     const:

     Foo some_function () {}
}

> IMO that isn't consistent. IMO only the last is valid.
> With this example, it is somewhat understandable.
>
> // C++
> class Bar {
>      const Foo getFooObj() const { ... }
>      const Foo& getFooObjRef const { ... }
> };
>
> // D
> class Bar {
>      const(Foo) getFooObj() const { ... }
>      ref Foo getObjRef() { ... }
> }
>
> const(Foo) but ref Foo. This is inconsistency, if you ask me.
> So why is between C++ and D such a huge difference?
> Why isn't it simply const Foo instead of const(Foo)?

I think the reason is the same as above. If the return value is const 
you need to use parentheses. I think that the syntax would conflict with 
the const-method syntax otherwise.

The reason for why const is allowed after the paramter list is because 
it can be a bit confusing to have two const next to each other.

class Foo
{
     const const (Foo) foo () {}
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list