Quantcast
Viewing all articles
Browse latest Browse all 5

Answer by Pubby for handling void type in template

If all you're doing is taking and releasing a lock then you should use RAII instead of calling lock/unlock functions. This is especially true if Func could throw, as if it does the code after it will not be called.

Once you have an RAII lock (or some time of RAII object if you need more than locks), you can simply do this which works for void:

template<typename T>T Foo( T(*Func)() ) {    lock my_lock;    return Func();}

template<typename T>T Foo( T(*Func)() ) {    struct raii_wrapper {      raii_wrapper(T(*Func)()) : Func(Func) {        // pre effects      }      ~raii_wrapper() {        // post effects      }      T(*Func)();    } actions(Func);    return Func;}

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>