C++ side effects

B

baibaichen

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
 
A

Abhishek Padmanabh

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top