rigtorp / MPMCQueue

A bounded multi-producer multi-consumer concurrent queue written in C++11
MIT License
1.15k stars 159 forks source link

Beginner Question #24

Closed baylf2000 closed 2 years ago

baylf2000 commented 3 years ago

I'm running into a compile issue using a struct containing a std::string as the type when constructing a new MPMCQueue.

For example, using a struct:

struct MyEvent {
    std::string MyString;
    long MyLong;
};

and trying to construct a MPMCQueue:

MPMCQueue<MyEvent> EventQueue(100);

I get a compiler message:

"T must be nothrow copy constructible".

If I remove the std::string from the struct and replace it with another long for example, it compiles and works perfectly. It also works perfectly if I use just a std::string in constructing the queue. But not when it's used inside the struct.

Any advice would be appreciated.

rigtorp commented 3 years ago

The limitation is because this line cannot throw: https://github.com/rigtorp/MPMCQueue/blob/master/include/rigtorp/MPMCQueue.h#L185. std::string copy constructor can throw if allocation fails.

If you move MyEvent into the queue using try_emplace(std::move(myEvent)) it should work.