C++ by-reference argument and C linkage

前端 未结 5 2097
执笔经年
执笔经年 2021-01-08 00:07

I have encountered a working (with XLC8 and MSFT9 compilers) piece of code, containing a C++ file with a function defined with C linkage and a reference argument. This bugs

5条回答
  •  不思量自难忘°
    2021-01-08 00:37

    That's what Bjarne Stroustrup has to say about references:

    "Most references are implements are implemented using a pointer variable; that it a reference usually takes up one word of memory. However, a reference that is used purely locally can - and often is - eliminated by the optimizer".

    For example:

     struct S 
     { 
        int a;
        int b[100]; 
     };  // just an example
    
     void do_something(const vector& v)
     {
        for (int i=0; i

    In this case, p needs not be stored in memory (maybe it just exists in a register, maybe it disappears into the instructions).

提交回复
热议问题