C++ side effects

Discussion in 'C++' started by baibaichen, Apr 8, 2008.

  1. baibaichen

    baibaichen Guest

    hi All

    the following comes from c++ standards:

    "Accessing an object designated by a volatile lvalue (3.10), modifying
    an object, calling a library I/O
    function, or calling a function that does any of those operations are
    all side effects, which are changes in the
    state of the execution environment."

    According to this definition, consider this function,

    T ReturnT(){
    T result_;
    //do something
    return result_; // #1
    }

    At #1, I think
    1) if T is scalre type such as int or char, the returning sentence
    would not have side effects
    2) if T is a class type, does returning type T have side effects? and
    Why? I.e. which program state is changed in the T's copy constrcutor.

    Thanks
    Chang
     
    baibaichen, Apr 8, 2008
    #1
    1. Advertising

  2. On Apr 8, 3:42 pm, baibaichen <> wrote:
    > the following comes from c++ standards:
    >
    > "Accessing an object designated by a volatile lvalue (3.10), modifying
    > an object, calling a library I/O
    > function, or calling a function that does any of those operations are
    > all side effects, which are changes in the
    > state of the execution environment."
    >
    > According to this definition, consider this function,
    >
    > T ReturnT(){
    >     T result_;
    >     //do something
    >    return result_;    // #1
    >
    > }
    >
    > At #1, I think
    > 1) if T is scalre type such as int or char, the returning sentence
    > would not have side effects
    > 2) if T is a class type,  does returning type T have side effects? and
    > Why? I.e. which program state is changed in the T's copy constrcutor.


    Could be anything depending upon what's written in the copy
    constructor for the specific type T. No? For example, even a simple
    print to console or writing something to a log file (both I/O related)
    could be considered a side-effect. Copies as in above can be elided
    via RVO/NRVO alongwith those side-effects.
     
    Abhishek Padmanabh, Apr 8, 2008
    #2
    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. Jim Bancroft
    Replies:
    2
    Views:
    2,438
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=
    Dec 28, 2004
  2. Anonieko
    Replies:
    0
    Views:
    1,965
    Anonieko
    Aug 12, 2005
  3. VisionSet
    Replies:
    7
    Views:
    481
    Michael Rauscher
    Nov 9, 2004
  4. Jason Heyes
    Replies:
    10
    Views:
    549
    red floyd
    Dec 21, 2004
  5. Giuseppe

    Re: Newbie-Side effects?

    Giuseppe, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,171
    Giuseppe
    Jun 24, 2003
Loading...

Share This Page