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
  • 449,505
  • 63
  • 781
  • 982
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277

0 Answers0