Passing non-const references to rvalues in C++

前端 未结 7 1006
长发绾君心
长发绾君心 2020-12-09 18:28

In the following line of code:

bootrec_reset(File(path, size, off), blksize);

Calling a function with prototype:

static void b         


        
7条回答
  •  悲哀的现实
    2020-12-09 19:12

    1. Is a fairly arbitrary decision - non-const references to temporaries are allowed when the temporary is the subject of a method call, for example (e.g. the "swap trick" to free the memory allocated by a vector, std::vector().swap(some_vector);)
    2. Short of giving the temporary a name, I don't think you can.
    3. As far as I'm aware this rule exists in C++0x too (for regular references), but rvalue references specifically exist so you can bind references to temporaries - so changing bootrec_reset to take a File && should make the code legal.

提交回复
热议问题