Re: Boxing and Unboxing ??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Bruce Wood wrote:
Jesse McGrew wrote:
Peter Olcott wrote:
"Jesse McGrew" <jmcgrew@xxxxxxxxx> wrote in message
news:1168918374.578116.244610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Why should the programmer have to change his code just to enable an
optimization? Why not just say "your methods will run faster if you
don't modify large value-type parameters"?

This aspect is not a matter of optimization, it is a matter of preventing
programming errors. By using the [const] parameter qualifier it is impossible to
inadvertently change a function parameter that was not intended to be changed.

It's already impossible! Those errors don't exist today: if you pass a
value type into a method, any changes the method makes will only affect
the local copy inside that method; the original value in the caller
won't be changed.

The potential errors are purely a side effect of the *optimization*
you've been proposing, which is to pass "in" value types by reference
even though you don't expect to get a changed value back from the
method (which is the purpose of "ref" and "out" parameters). If you
weren't passing them by reference, there'd be no need for those
parameters to be read-only, because any changes would be local to the
method being called.

Jesse, I think that you misunderstand. C++ has a concept of "const" by
which you can pass a reference type, but can indicate that none of its
fields can be changed.

Maybe I am misunderstanding Peter's suggestion, then. I thought he was
talking about providing this for value types, such that 'void Foo(in
LargeStruct bar)' would pass bar by reference like the "ref" keyword,
but it would then be read-only from within Foo.

I can see the merits of applying a "const" attribute to reference types
- although read-only wrappers seem to be a decent solution too.

Jesse

.



Relevant Pages

  • Re: Setting PropertySheet Title (Wizard mode)
    ... you really have to embrace "const" as a concept. ... about the pros and cons of passing small strings by reference vs. by ... If you think of it as optimization, it is probably the wrong reason. ... Not with CString. ...
    (microsoft.public.vc.mfc)
  • Re: Boxing and Unboxing ??
    ... don't modify large value-type parameters"? ... This aspect is not a matter of optimization, it is a matter of preventing ... which is to pass "in" value types by reference ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Boxing and Unboxing ??
    ... don't modify large value-type parameters"? ... This aspect is not a matter of optimization, it is a matter of preventing ... which is to pass "in" value types by reference ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to write an operator
    ... We prefer if you reply to posts in place, that is include the quoted text ... In return a const object, we're talking about returned a value. ... returning a reference to an object is another story. ... an object by value, as with operator++ operator+, it doesn't really matter ...
    (comp.lang.cpp)
  • Re: Garbage collection problem
    ... The whole point of optimization is to ... > same camp as Chris Smith on the matter. ... declare a scope for the variable I may be depending on the object remaining ...
    (comp.lang.java.programmer)