Some Basic QUESTIONS.

Discussion in 'C++' started by kunjaan, Mar 24, 2009.

  1. kunjaan

    kunjaan Guest

    I was just going through the C++ STL and it says that "the STL was
    created with four ideas in mind: generic programming, abstractness
    without loss of efficiency, the Von Neumann computation model, and
    value semantics."

    1. What are value semantics?
    2. What kind of losses do you get with abstractions?
    kunjaan, Mar 24, 2009
    #1
    1. Advertising

  2. kunjaan

    Stefan Ram Guest

    Re: Some Basic QUESTIONS.

    kunjaan <> writes:
    >1. What are value semantics?


    An object represents an immutable value (vs. an object
    represents a system with a mutable state.)

    >2. What kind of losses do you get with abstractions?


    Evaluating (( lambda( x )( add x x ))2 ) at runtime usually
    will be less efficient than evaluating ( add 2 2 ).
    Stefan Ram, Mar 24, 2009
    #2
    1. Advertising

  3. kunjaan

    James Kanze Guest

    Re: Some Basic QUESTIONS.

    On Mar 25, 12:44 am, -berlin.de (Stefan Ram) wrote:
    > kunjaan <> writes:
    > >1. What are value semantics?


    > An object represents an immutable value (vs. an object
    > represents a system with a mutable state.)


    Not necessarily, at least not in C++. I do like the idea that
    an object with value semantics can only be modified by the
    assignment operators, but this is far from the general
    case---std::string, for example, clearly has value semantics,
    despite a large number of mutator functions. (One can argue
    that this is a design error, but if so, it's still one we have
    to live with.)

    The important consideration for value semantics is that only the
    value of an object is significant, not its identity. So you can
    copy it (copy constructor or assignment) as much as you like,
    and any copy can be used in place of the original with no change
    in the behavior of the program. Arguably, this implies
    immutability (except by assignment), since any changes will only
    affect the copy, but in practice, with care, mutable types
    like std::string do seem to be usable as values as well.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 25, 2009
    #3
  4. kunjaan

    Stefan Ram Guest

    Re: Some Basic QUESTIONS.

    James Kanze <> writes:
    >The important consideration for value semantics is that only the
    >value of an object is significant, not its identity. So you can
    >copy it (copy constructor or assignment) as much as you like,


    One can eventually identify five different concepts that can
    be used to explain this notion in a more precise manner, these
    are: extensionality, definiteness, substitutivity of identity,
    unfoldability, and referential transparency.

    More details:

    http://www.purl.org/stefan_ram/pub/referential_transparency
    Stefan Ram, Mar 25, 2009
    #4
  5. kunjaan

    Stefan Ram Guest

    Re: Some Basic QUESTIONS.

    James Kanze <> writes:
    >only the value of an object is significant, not its identity.


    Since I wrote »more precise« in my previous post, I need to
    explain my problems with the above wording regarding »value
    semantics«.

    I am not sure what »the value of an object« versus »the
    identity of an object« is, and what »significant« means here.
    I believe that none of these notions is given by ISO/IEC
    14882:2003(E).

    Since an object is a typed region of storage, the »value of an
    object« might be taken to be the state of this storage, thus,
    »the value« is a composite value in the case of a class with
    multiple fields. Such a thing might not be a »first-class
    value« in C++, I believe it is represented by a so-called
    »temporary object« when it is being transferred.

    (Below, I choose to avoid the term »value« and use »==«
    instead.)

    »The identity of an object« might be read as »the address of
    an object«.

    »significant« might mean »able to change the observable
    behavior of the program«.

    Thus, the above possibly might be worded as:

    The observable behavior of the evaluation of a function
    call »f( o )« with a function »f« that has value-semantics
    with regard to an argument object »o« will not change,
    when the call is being replace by »f( o_ )«, given that
    »o_« has the same type as »o« and »( o == o_ )« and that
    »f( o_ )« is being evaluated with the same state of the
    environment as »f( o )«.

    This usually implies that the behavior of »f« does not
    depend on the address of this argument.
    Stefan Ram, Mar 25, 2009
    #5
  6. kunjaan

    Stefan Ram Guest

    Re: [PS]Some Basic QUESTIONS.

    -berlin.de (Stefan Ram) writes:
    >»o_« has the same type as »o« and »( o == o_ )« and that


    Oops: I assumed that »==« was overloaded to give »value
    equality« for this class, but does not compare the addresses.
    (I am not sure whether this is common usage in C++.)
    Stefan Ram, Mar 25, 2009
    #6
  7. kunjaan

    James Kanze Guest

    Re: Some Basic QUESTIONS.

    On Mar 25, 3:23 pm, -berlin.de (Stefan Ram) wrote:
    > James Kanze <> writes:
    > >only the value of an object is significant, not its identity.


    > Since I wrote »more precise« in my previous post, I need to
    > explain my problems with the above wording regarding »value
    > semantics«.


    > I am not sure what »the value of an object« versus »the
    > identity of an object« is, and what »significant« means
    > here. I believe that none of these notions is given by
    > ISO/IEC 14882:2003(E).


    My use doesn't really concern what the stanard says. It's more
    of a design issue. And yes, the concept isn't really that
    precise: on can break it down into different aspects (as was
    done in the article you linked to in your other response). In
    practice, however, you usually don't have to---although
    conceptually, these aspects are orthogonal, in practice, most of
    the time, you will design an object to support all of them, or
    non.

    > Since an object is a typed region of storage, the »value of an
    > object« might be taken to be the state of this storage, thus,
    > »the value« is a composite value in the case of a class with
    > multiple fields. Such a thing might not be a »first-class
    > value« in C++, I believe it is represented by a so-called
    > »temporary object« when it is being transferred.


    The standard is a little vague in this regard. The "value" of a
    POD is basically the bit pattern in its bytes (except that in
    some cases, multiple bit patterns may be considered equal). The
    value of a non-POD is intentionally left vague---the idea is
    that it is up to the programmer to define what he means by it.
    (Note that this makes the specification of concepts like
    CopyConstructible or Assignable vague as well. Again, this is
    largely intentional; its up to the user to decide what
    "equivalent" means, or should mean, in his case, and implement
    the various constructors and operators accordingly.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 26, 2009
    #7
  8. kunjaan wrote, On 25.3.2009 0:32:
    > I was just going through the C++ STL and it says that "the STL was
    > created with four ideas in mind: generic programming, abstractness
    > without loss of efficiency, the Von Neumann computation model, and
    > value semantics."
    >
    > 1. What are value semantics?

    Compare value semantics and pointer/reference semantics. If you have value
    semantics, you assume that for some type T and variables A, B of the type T,
    if you do A = B and then you modify B, variable A will not be affected in any
    way. Opposite to that, with pointer/reference semantics (in language like
    Java or Python) assignment A = B makes A point/refer to the same value as B
    does, it does not make a copy of it. Any changes then done on/through B will
    also be visible through A.

    > 2. What kind of losses do you get with abstractions?

    Abstraction often means introducing some form of indirection, often pointers.
    In C++, abstraction can be achieved using virtual member functions.
    Unfortunatelly, using virtual member functions usually means that compilers
    cannot do inlining and other optimizations. That is loss of efficiency caused
    by abstraction.

    OTOH, abstracting things using generic programming and templates means that
    compilers can see through all the code and function calls and can do inlining
    and many other optimizations that would be inhibited by use of virtual member
    functions.

    --
    VH
    Vaclav Haisman, Mar 29, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Klett

    <div> tags, some basic questions

    Steve Klett, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    357
    Kevin Spencer
    Nov 13, 2003
  2. Michael Hesse
    Replies:
    6
    Views:
    437
    Thomas Weidenfeller
    Jul 26, 2005
  3. ccs

    Some basic questions

    ccs, Jun 11, 2004, in forum: C++
    Replies:
    6
    Views:
    342
    Mike Wahler
    Jun 11, 2004
  4. slot

    Some basic questions

    slot, Aug 3, 2004, in forum: C++
    Replies:
    8
    Views:
    358
    Default User
    Aug 3, 2004
  5. Player

    some basic questions...

    Player, Sep 20, 2004, in forum: Python
    Replies:
    3
    Views:
    363
Loading...

Share This Page