首先是一个小小的上下文:我正在学习C++ 11中的线程,为此目的,我正在尝试构建一个actor小类,基本上(我将异常处理和传播内容留下),如下所示:
class actor {
private: std::atomic<bool> stop;
private: std::condition_variable interrupt;
private: std::thread actor_thread;
private: message_queue incoming_msgs;
public: actor()
: stop(false),
actor_thread([&]{ run_actor(); })
{}
public: virtual ~actor() {
// if the actor is destroyed, we must ensure the thread dies too
stop = true;
// to this end, we have to interrupt the actor thread which is most probably
// waiting on the incoming_msgs queue:
interrupt.notify_all();
actor_thread.join();
}
private: virtual void run_actor() {
try { …Run Code Online (Sandbox Code Playgroud) 我正在用 C++14 绘制一个小的通用类型包装器模板,它旨在使用 mixins 启用、禁用或扩展底层类型的接口。
这是这个包装器的代码(精简了一点):
namespace detail {
/// Helper template that is forwarded to the mixins used
/// for the extension of wrapper<•> in order to enable
/// access to the actual type `Derived`
template <typename Derived>
struct Cast {
using type = Derived;
template <typename T>
static constexpr Derived& self(T* self) { return *static_cast<Derived*>(self); }
template <typename T>
static constexpr Derived const& self(T const* self) { return *static_cast<Derived const*>(self); }
};
/// This helper template is used …Run Code Online (Sandbox Code Playgroud) c++ constructor explicit-constructor implicit-conversion c++14