metaprogramming

Wrap function implementations returning a specific type into another function programatically

℡╲_俬逩灬. 提交于 2020-12-29 08:07:32
问题 I would like to wrap all the user defined functions in a scala project that return a certain type T , into a function that accepts a T and the function name as parameters. eg. given this function is in scope: def withMetrics[T](functionName: String)(f: => Try[T]): Try[T] = { f match { case _: Success[T] => println(s"send metric: success for $functionName") case _: Failure[T] => println(s"send metric: failure for $functionName") } f } the user can send metrics for their functions which return

Why do I get an error trying to call a template member function with an explicit type parameter?

流过昼夜 提交于 2020-11-30 02:14:11
问题 I don't get it, it seems to me that the call to f is completely unambiguous, but it fails to compile with expected primary-expression before ‘int’ . If I comment out the line with the call to f , it compiles fine. template<typename T> struct A { template<typename S> void f() { } }; template<typename T> struct B : A<T> { void g() { this->f<int>(); } }; 回答1: This is due to a really obscure provision of the standard in which if you have a template that tries to access a template function in an