0

I'm learning how to use lambda functions. Why doesn't my last lambda run automatically if not assigned to a variable? All the previous ones work as expected.

#include "stdafx.h"
#include <iostream>
#include <thread>
using namespace std;
int main()
{
    std::thread t([]() {
        std::cout << "thread function\n";
    });
    std::cout << "main thread\n";
    t.join();

    auto a = []() -> int {
        return 1 + 2;
    };
    int n = a();
    cout << n << endl;

    auto b = []() -> void {
        cout << "sup" << endl;
    };
    b();

    //why doesn't this run automatically?
    []() -> void {
        cout << "hellur!" << endl;
    };

    return 0;
}
nhershy
  • 643
  • 1
  • 6
  • 20

1 Answers1

1

Solution:

[]() -> void {
    cout << "hellur!" << endl;
}();
nhershy
  • 643
  • 1
  • 6
  • 20