Skip to main content
added 26 characters in body
Source Link
Remy Lebeau
  • 592.7k
  • 33
  • 492
  • 832

I am learning C++14 lambdas with const, and today my friend showed me thisthe following. I could not understand 1. Is it a lambda function but the syntax does not matches what I see usually and the 2. syntax matches with lambda function but it fails with a long error.

  1. Is it a lambda function? The syntax does not matches what I see usually.

  2. it syntax matches with a lambda function, but it fails with a long error.

int main()
{
    
    // 1.
    const auto x = [&]{
        auto l = 0;
        l = 99;
        return l;
    }();
    
    std::cout << x << endl;
    
    // 2.    
    const auto y = [&](){
        auto l = 0;
        l = 99;
        return l;
    };
    
    std::cout << y << endl;   
    
    return 0;
}

I wantedwant to know what is 1. is, and why 2. fails to compile.

I am learning C++14 lambdas with const and today my friend showed me this. I could not understand 1. Is it a lambda function but the syntax does not matches what I see usually and the 2. syntax matches with lambda function but it fails with a long error.

int main()
{
    
    // 1.
    const auto x = [&]{
        auto l = 0;
        l = 99;
        return l;
    }();
    
    std::cout << x << endl;
    
    // 2.    
    const auto y = [&](){
    auto l = 0;
    l = 99;
    return l;
    };
    
    std::cout << y << endl;   
    
    return 0;
}

I wanted to know what is 1. and why 2. fails to compile.

I am learning C++14 lambdas with const, and today my friend showed me the following. I could not understand it.

  1. Is it a lambda function? The syntax does not matches what I see usually.

  2. it syntax matches with a lambda function, but it fails with a long error.

int main()
{
    // 1.
    const auto x = [&]{
        auto l = 0;
        l = 99;
        return l;
    }();
    
    std::cout << x << endl;
    
    // 2.    
    const auto y = [&](){
        auto l = 0;
        l = 99;
        return l;
    };
    
    std::cout << y << endl;   
    
    return 0;
}

I want to know what 1 is, and why 2 fails to compile.

edited tags
Link
max66
  • 66.1k
  • 11
  • 73
  • 117
deleted 48 characters in body
Source Link

I am learning C++14 lambdas with const and today my friend showed me this. I could not understand 1. Is it a lambda function but the syntax does not matches what I see usually and the 2. syntax matches with lambda function but it fails with a long error.

int main()
{
    
    // 1.
    const auto x = [&]{
        auto const_vall = 0;
        const_vall = 99;
        return const_val;l;
    }();
    
    std::cout << x << endl;
    
    // 2.    
    const auto y = [&](){
    auto const_vall = 0;
    const_vall = 99;
    return const_val;l;
    };
    
    std::cout << y << endl;   
    
    return 0;
}

I wanted to know what is 1. and why 2. fails to compile.

I am learning C++14 lambdas with const and today my friend showed me this. I could not understand 1. Is it a lambda function but the syntax does not matches what I see usually and the 2. syntax matches with lambda function but it fails with a long error.

int main()
{
    
    // 1.
    const auto x = [&]{
        auto const_val = 0;
        const_val = 99;
        return const_val;
    }();
    
    std::cout << x << endl;
    
    // 2.    
    const auto y = [&](){
    auto const_val = 0;
    const_val = 99;
    return const_val;
    };
    
    std::cout << y << endl;   
    
    return 0;
}

I wanted to know what is 1. and why 2. fails to compile.

I am learning C++14 lambdas with const and today my friend showed me this. I could not understand 1. Is it a lambda function but the syntax does not matches what I see usually and the 2. syntax matches with lambda function but it fails with a long error.

int main()
{
    
    // 1.
    const auto x = [&]{
        auto l = 0;
        l = 99;
        return l;
    }();
    
    std::cout << x << endl;
    
    // 2.    
    const auto y = [&](){
    auto l = 0;
    l = 99;
    return l;
    };
    
    std::cout << y << endl;   
    
    return 0;
}

I wanted to know what is 1. and why 2. fails to compile.

Source Link
Loading