What is the return type of a lambda expression if an item of a vector is returned?

后端 未结 3 1521
无人及你
无人及你 2020-12-10 10:24

Consider the following snippet:

#include 
#include 
#include 

int main() 
{
    std::vectorv = {         


        
3条回答
  •  青春惊慌失措
    2020-12-10 11:17

    1. I assume the first lambda might return int or int&¹. Not const int& as the reference v is not to a const object (it doesn't matter that the capture itself is not mutable, since that's the reference itself)

    2. Life times of temporaries get extended to the end of the enclosing scope when bound to a const-ref


    ¹ I'll try to dive into that later. For now, I'm going with regular deduction intuition and say that auto would decuce int, whereas auto& would deduce int&, so I expect that the actual return type is int. If anyone beats me to it, all the better. I won't have time for a few hours

    See the test supplied by @milleniumbug: Live On Coliru

提交回复
热议问题