Old problem with performance

Don nospam at nospam.com
Mon Feb 23 08:17:57 PST 2009


Weed wrote:
> naryl пишет:
> 
>>>>>>> --bb
>>>>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>>>>> You should use a struct there!   Your code does not show you doing
>>>>> anything that would even remotely suggest using a class is worthwhile.
>>>>>  You're doing value operations on value types.  That's what structs
>>>>> are for.
>>>>  	
>>>> Why?
>>>>
>>>> What if I have not substantiated the fact that c1 is a class I should
>>>> use there a structure?
>>>>
>>>> Used in D a model of placement classes only in heap have a rule "if you
>>>> made the class and trying to pass it by value is somewhere in your code,
>>>> there is a design error?
>>> Explains why the question is given in this form:
>>>
>>> I am received or wrote a classes. Is it right to overload the operator
>>> opAdd and use them? I think yes.
>>>
>>> But why not allow this operation at the same speed that allows C++?
>> If you pass it by value you'll lose polymorphism.
> 
> Debatable
> 
> By the way, in my example the transfer class by value is not
> important(!), it is important to create a temporary class on the stack.
> Then we can pass a reference without problem (C++ allows this through
> the "&")

Sure, but you're not using polymorphism.

> 
>> That must mean that you inherit that class only to avoid duplicating code. And that is easily done with template mixins.
> 
> It is possible that this polymorphism is not needed and should be
> prohibited for operations by value. The class is ready, why it should
> not be used and have to redo it?
> 
> Can not think about changing when the class is ready - he could have
> enormous complexity and hierarchy.

This is the fundamental tradeoff at the heart of the matter.
In D, the choice of whether an object will use polymorphism or not is 
considered a fundamental design decision. D gets significant benefits 
from this. C++ allows you to defer the decision, but it doesn't come for 
free.
(Generally speaking, statically typed languages do force you to make 
more decisions at design time). Notice that you have little gotchas in 
C++, such as the need to declare a virtual destructor on every struct 
you think you might "someday" use polymorphically. It sticks around, 
even if it never gets used.
One of the nice things about a D struct, compared to a C++ struct, is 
that you *know* it's simple, it never has that kind of baggage.

D does choose different trade-offs from C++. If it was always the same, 
it'd be the same language!

BTW, if you're concerned about performance, you'd do well to use 
compile-time polymorphism rather than run-time, when possible. D's 
metaprogramming support leaves C++ for dead.




More information about the Digitalmars-d mailing list