Is there a safe way to have a std::thread as a member of a class?

You can use a thread in combination with move semantics: class MyClass final { private: std::thread mythread; void _ThreadMain(); public: MyClass() : mythread{} // default constructor { // move assignment mythread = std::thread{&MyClass::_ThreadMain, this}; } }; The move assignment operator is documented on the following page. In particular, it is noexcept and no new thread … Read more

Get companion object of class by given generic type Scala

A gist by Miles Sabin may give you a hint: trait Companion[T] { type C def apply() : C } object Companion { implicit def companion[T](implicit comp : Companion[T]) = comp() } object TestCompanion { trait Foo object Foo { def bar = “wibble” // Per-companion boilerplate for access via implicit resolution implicit def companion … Read more

Scala’s .type and Java’s .class literal

Here is my rationalization: classOf[T] classOf is defined in Predef as a function with this signature: def classOf[T]: Class[T] Although it’s implemented by the compiler, using the function syntax is possible without having to create any special treatment in terms of syntax. So that’s one reason here to consider this option. The alternative like String.class … Read more