Jump to page: 1 2
Thread overview
Re: const methods()
Jan 23, 2007
renoX
Jan 23, 2007
Mike
Jan 23, 2007
Kristian Kilpi
Jan 23, 2007
Mike
Jan 23, 2007
Kristian Kilpi
Jan 23, 2007
James Dennett
Jan 23, 2007
Kristian Kilpi
Jan 24, 2007
Bill Baxter
Jan 24, 2007
Kristian Kilpi
Jan 24, 2007
Miles
January 23, 2007
Andrey Khropov Wrote:
> Frits van Bommel wrote:
> > the way C++ does const sucks :P.
> Why?

I remember that this is common knowledge, but I don't remember why.

If memory serves, this is because in C++ you can 'unconst' parameters with a cast, so the compiler cannot really optimise the code.
January 23, 2007
Well even the C++ way worked pretty well. Whatever method is used we need a solution to this problem.

Walter are you going to introduce a fix to this problem?
January 23, 2007
On Tue, 23 Jan 2007 12:18:28 +0200, renoX <renosky@free.fr> wrote:
> Andrey Khropov Wrote:
>> Frits van Bommel wrote:
>> > the way C++ does const sucks :P.
>> Why?
>
> I remember that this is common knowledge, but I don't remember why.
>
> If memory serves, this is because in C++ you can 'unconst' parameters with a cast, so the compiler cannot really optimise the code.

One thing I hate with C++'s const, sometimes, is that it applies to the whole class, not just the part that's visible to the user.

For example:

  class MyClass {
  public:
    int getVal() const {
      update_cache();  //(error, not const)
      return m_cache.val;
    }

  protected:
    void update_cache() {
      if(m_cache.is_dirty) {
        m_cache.is_dirty = false;
        m_cache.val = ...;
      }
    }

    my_cache_t m_cache;
  };

I would like to have different 'layers of constness'. E.g. one layer guarantees that a method doesn't alter the public state of the class, the second layer that the non-public state is not changed.

In the example above, one have to do an 'unconst' in 'getVal()':

  ((MyClass *)this)->update_cache();

When carefully used it's useful, and necessary for 'greater good'. ;)
January 23, 2007
Kristian, the mutable keyword allows you to override the constness. So in my opinion, the way C++ does const objects is perfect. Cant understand why its not in D.
January 23, 2007
On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike@mike.com> wrote:
> Kristian, the mutable keyword allows you to override the constness. So in my
> opinion, the way C++ does const objects is perfect. Cant understand why its not in D.

Wha!? I can't believe I have missed that one! 8|  Hehheh... well, you learn every day something new (sometimes pretty essential things it seems)... Thanks for pointing this out. :)
January 23, 2007
Kristian Kilpi wrote:
> On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike@mike.com> wrote:
>> Kristian, the mutable keyword allows you to override the constness. So
>> in my
>> opinion, the way C++ does const objects is perfect. Cant understand
>> why its not in D.
> 
> Wha!? I can't believe I have missed that one! 8|  Hehheh... well, you learn every day something new (sometimes pretty essential things it seems)... Thanks for pointing this out. :)

Without wishing to pick on Kristian, this is fairly common
of "your language sucks because..." arguments -- those who
know the language better know the argument to be based on
insufficient understanding, but that doesn't stop it being
promulgated enthusiastically.  Right now this probably
happens more to C and C++ than to any other languages
(which isn't to say that those languages don't have
plenty of legitimate warts), but if/when D has more success
the D community will likely be on the receiving end of this.

Learning from how this has hurt other languages could enable the D community to manage the propaganda war more effectively.

-- James
January 23, 2007
On Tue, 23 Jan 2007 18:15:14 +0200, James Dennett <jdennett@acm.org> wrote:
> Kristian Kilpi wrote:
>> On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike@mike.com> wrote:
>>> Kristian, the mutable keyword allows you to override the constness. So
>>> in my
>>> opinion, the way C++ does const objects is perfect. Cant understand
>>> why its not in D.
>>
>> Wha!? I can't believe I have missed that one! 8|  Hehheh... well, you
>> learn every day something new (sometimes pretty essential things it
>> seems)... Thanks for pointing this out. :)
>
> Without wishing to pick on Kristian, this is fairly common
> of "your language sucks because..." arguments -- those who
> know the language better know the argument to be based on
> insufficient understanding, but that doesn't stop it being
> promulgated enthusiastically.  Right now this probably
> happens more to C and C++ than to any other languages
> (which isn't to say that those languages don't have
> plenty of legitimate warts), but if/when D has more success
> the D community will likely be on the receiving end of this.
>
> Learning from how this has hurt other languages could
> enable the D community to manage the propaganda war more
> effectively.
>
> -- James

Thanks (for not picking on me). Actually I have used C++ a lot and done preeetty big projects with it. It's amazing that I hadn't noticed the 'mutable' keyword during all these years. When I came across a situation I described earlier in my example, the first thing I thought of was to do a nonconst cast and be done with it. That is, C++ allows you to do that. If nonconst casting had not been possible, then I would have searched for different solution, and noticed that 'mutable' exists.

But yeah, partial understanding of a language can cause "your language sucks" remarks. But maybe the first impression of the language wasn't the best possible then. For example, lisp is not for me. I mean, it's a great language with cool features etc, but I have found it not-so-easy to code with. Of course, a couple of years of hard coding with it would probably change that. On the other hand, some features and limitations of a language can begin to annoy you after a while. For instance, I have learnt to *hate* the header files of C and C++.

And, of course, if someone says "that sucks" without explaining why is that, nobody should notice that remark. I did explain what was unfortunate with C++ const in my opinion, and I was 'corrected' right away. <g>
January 24, 2007
Mike wrote:
> So in my
> opinion, the way C++ does const objects is perfect. Cant understand why its not in D.

Me neither.

I read this newsgroup for some years, and have never understood why D doesn't have const members, other than "Walter doesn't like them".

There is a teaching that says that we should not try to fix what is not broken. So, IMO, while nobody manages to figure out what is broken in C++'s const, we should just stick with it. It works exactly how it is intended to, and does a good job. Just adopt it.
January 24, 2007
Miles wrote:
> Mike wrote:
>> So in my
>> opinion, the way C++ does const objects is perfect. Cant understand why its not in D.
> 
> Me neither.
> 
> I read this newsgroup for some years, and have never understood why D
> doesn't have const members, other than "Walter doesn't like them".
> 
> There is a teaching that says that we should not try to fix what is not
> broken. So, IMO, while nobody manages to figure out what is broken in
> C++'s const, we should just stick with it. It works exactly how it is
> intended to, and does a good job. Just adopt it.

C++'s const is not "broken" by a certain definition of broken, but it certainly can be vastly improved. I'm not sure how this idea that "nobody" figured out what the issues are with C++'s const came about.

Andrei
January 24, 2007
Kristian Kilpi wrote:
> On Tue, 23 Jan 2007 18:15:14 +0200, James Dennett <jdennett@acm.org> wrote:

> Thanks (for not picking on me). Actually I have used C++ a lot and done preeetty big projects with it. It's amazing that I hadn't noticed the 'mutable' keyword during all these years. When I came across a situation I described earlier in my example, the first thing I thought of was to do a nonconst cast and be done with it. That is, C++ allows you to do that. If nonconst casting had not been possible, then I would have searched for different solution, and noticed that 'mutable' exists.

If you learned C++ pretty early on then it's not too surprising that you weren't aware of 'mutable'.  It was a fairly late addition.  I think the first copy of Stroustrup I bought did not have 'mutable'.

--bb
« First   ‹ Prev
1 2