1

I know that I can use emplace_back, but I would prefer to use initializer list if possible...

#include <iostream>
#include <thread>
#include <vector>
    int main(){
        std::vector<std::thread> threads {
            //{[](){std::cout << "HELL";}},
            //{[](){std::cout << "O";}}}
        };
        std::vector<std::thread> threads2;
        threads2.emplace_back([](){std::cout << "HELL";});
        threads2.emplace_back([](){std::cout << "O";});
    }

Uncommenting commented lines causes compile failure, I presume because initializer list values are normal references and not rvalue references so vector constructor tries to copy noncopyable std::thread.

Is there any way to make initializer list version work?

note: this is toy example, I know I have a race between writes to cout in my example.

Nicol Bolas
  • 413,367
  • 61
  • 711
  • 904
NoSenseEtAl
  • 26,625
  • 26
  • 107
  • 242

0 Answers0