c++ - Is pass-by-value/reference defined implementation or behavior wise? -


q: pass-by-value/reference defined strictly behavior or implementation wise in c++, , can provide authoritative citation?

i had conversion friend pass-by-value/reference in c++. came disagreement on definition of pass-by-value/reference. understand passing pointer function still pass-by-value since value of pointer copied, , copy used in function. subsequently, dereferencing pointer in function , mutating modify original variable. disagreement appears.

his stance: because pointer value copied , passed function, performing operations on dereferenced pointer has ability affect original variable, has behavior of pass-by-reference, passing pointer function.

my stance: passing pointer function copy value of pointer, , operations in function may affect original variable; however, because may affect original, behavior not constitute pass-by-reference since implementation of language defines these terms, pass-by-value/reference.

quoting definition given highest voted answer here: language agnostic

pass reference

when parameter passed reference, caller , callee use same variable parameter. if callee modifies parameter variable, effect visible caller's variable.

pass value

when parameter passed value, caller , callee have two independent variables same value. if callee modifies parameter variable, effect not visible caller.

i still have ambiguous feeling after reading these. example, pass value/reference quotes can support either of our claims. can clear definitions of whether these definition stem behavior or implementation , provide citation? thanks!

edit: should little more careful of vocabulary. let me extend question clarification. mean when questioning pass-by-reference not talking purely c++ implementation of & reference, instead theory. in c++, & pass-by-reference true pbr because not can modify original value, memory address of value. leads this, example pointers count pbr?

void foo(int ** bar){     *bar = *bar+(sizeof(int*));     cout<<"inside:"<<*bar<<endl; }  int main(){     int = 42;     int* ptra = &a;     cout<<"before"<<ptra<<endl;     foo(&ptra);     cout<<"after:"<<ptra<<endl; } 

the output after ptra equal inside, meaning not can function modify a, ptra. because of this, define call-by-reference theory: being able not modify value, memory address of value. sorry convoluted example.

you talk lot pointers here, indeed passed value of time, don't mention actual c++ references, actual references.

int a{}; int& b = a;  // prints true std::cout << std::boolalpha << (&b == &a) << std::endl; 

here, can see, both variables have same address. put simply, in case, references act being name variable.

references in c++ special. not objects, unlike pointers. cannot have array of references, because require references has size. reference not required have storage @ all.

what passing variable reference then?

take @ code:

void foo(int& i) {     i++; }  int main() {     int i{};      foo(i);      // prints 1     std::cout << << std::endl; } 

in particular case, compiler must have way send variable reference bound. indeed references not required have storage, not required not have 1 either. in case, if optimizations disabled, compiler implements behavior of references using pointers.

of course, if optimizations enabled, may skip passing , inline function. in case, reference don't exist, or don't have storage, because original variable used directly.

other similar optimization happens pointers too, that's not point: point is, way references implemented implementation defined. implemented in term of pointers, not forced to, , way reference implemented may vary case case. behavior of references defined standard, , pass-by-reference.


what pointers? count passing reference?

i no. pointers objects, int, or std::string. can pass reference pointer, allowing change original pointer.

however, pointers have reference semantics. not reference indeed, std::reference_wrapper not reference either, have reference semantics. wouldn't call passing pointer "passing reference", because don't have actual reference, indeed have reference semantics.

a lot of things have reference semantics, pointers, std::reference_wrapper, handle resource, gluint, handle opengl object, have reference semantics, not references. don't have reference actual object, can change pointed-to object through these handles.

there other articles , answers can read about. informative value , reference semantics.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -