Array and Rvalue

后端 未结 3 1453
傲寒
傲寒 2020-12-11 16:24

$4.2/1 - \"An lvalue or rvalue of type “array ofN T” or “array of unknown bound of T” can be converted to an rvalue of type “pointer to T.” The result

相关标签:
3条回答
  • 2020-12-11 16:33

    I'm not sure what you refer to by "initialization/declaration" in this context. In the following, the array is a prvalue

    template<typename T> using alias = T;
    
    int main() { return alias<int[]>{1, 2, 3}[0]; }
    

    This can be verified by decltype(alias<int[]>{1, 2, 3}) having the type int[3]. Creating arrays this way on the fly wasn't initially intended to work but slipped into the working draft by-the-way of related work on uniform initialization. When I realized that some paragraphs in the C++0x working draft disallow some special case of this on-the-fly creation of array temporaries while other paragraphs allow it, I sent a defect report to the C++ committee, which then on the basis of GCC's partially working implementation decided to fully support this.

    0 讨论(0)
  • 2020-12-11 16:43

    Would this stand a chance to demonstrate Array Rvalue?

    int main(){
     int buf[10][10];
    
     int (*p)[10] = buf;
    
     int (*p2)[10] = p;      // LValue to Rvalue conversion of Array type 'p'
    }
    
    0 讨论(0)
  • 2020-12-11 16:46

    You cannot get an rvalue of array type. Arrays can only be lvalues, and whenever they are used in an lvalue they decay to a pointer to the first element.

    int array[10];
    int * p = array; // [1]
    

    The expression array in [1] is an lvalue of type int (&)[10] that gets converted to an rvalue of type int *p, that is, the rvalue array of N==10 T==int is converted to an lvalue of type pointer to T==int.

    0 讨论(0)
提交回复
热议问题