Quantcast
Channel: handling void type in template - Stack Overflow
Viewing all articles
Browse latest Browse all 5

handling void type in template

$
0
0

I have a templated function that invokes another function and stores its return value, then does some work before returning the value. I'd like to extend this to handle T = void, and was wondering if specialization is my only option.

template<typename T>T Foo( T(*Func)() ) {    // do something first (e.g. some setup)    T result = Func();    // do something after (e.g. some tear down)    return result;}// Is this specialization the only option?template<>void Foo<void>( void(*Func)() ) {    // do something first (e.g. some setup)    Func();    // do something after (e.g. some tear down)    return;}void Bar() {}int BarInt() { return 1; }int main(){    Foo<int>(&BarInt);    Foo<void>(&Bar);}

Or can the regular version of Foo be modified to handle the void type and basically do nothing in that case? I was thinking that maybe my local result could be wrapped in a type that could handle void maybe, but could also see the assignment as being a deal-breaker.


Viewing all articles
Browse latest Browse all 5

Trending Articles



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