Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9097349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:05:23+00:00 2026-06-17T00:05:23+00:00

I have a templated function that invokes another function and stores its return value,

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:05:24+00:00Added an answer on June 17, 2026 at 12:05 am

    Given that your operation does not depend on the result of the function, you can do it without a specialization. It is ok for a function returning void to return an expression of type void. So the return part is not the troublesome one, but you need to figure out a way to do the pre and post operations. Constructors and destructors will help you there:

    struct do_something_helper
    {
        do_something_helper()
        {
            // do something first (e.g. take a lock)
        }
        ~do_something_helper()
        {
            // do something after (e.g. release a lock)
        }
    };
    

    Then you can write your function like this:

    template<typename T>
    T Foo( T(*Func)() ) 
    {
        do_something_helper _dummy_helper; // constructor called here
    
        return Func();
        // destructor called here
    }
    

    For a more general solution using lambdas as you commented, it could look like this:

    template< typename Pre, typename Post >
    struct scope_guard
    {
        scope_guard( Pre&& pre, Post&& post )
          : _post( std::forward< Post >( post ) )
        {
            pre();
        }
        ~scope_guard()
        {
            _post();
        }
    
        Post _post;
    };
    
    template< typename Pre, typename Post >
    scope_guard< Pre, Post > make_scope_guard( Pre&& pre, Post&& post )
    {
        return scope_guard< Pre, Post >( std::forward< Pre >( pre ), std::forward< Post >( post ) );
    }
    
    template<typename T>
    T Foo( T(*Func)() ) 
    {
        auto do_something_helper =
            make_scope_guard(
                [](){ /* do something first (e.g. take a lock) */ },
                [](){ /* do something after (e.g. release a lock) */ }
            );
    
        return Func();
    }
    

    A type-erased version using std::function< void() > would be easier to write and use, but it would be rather inefficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a templated math function which takes two values, does some math to
I currently have a function that is meant to return T (templated function). So
I have a templated function fct that uses some complex data structure based on
I have some simple javascript that as far as i can tell should work
I have a function that expects a templated iterator type. It currently dereferences the
I have a templated function that operates on a template-type variable, and if the
Suppose I have a templated function that deals with pointers to yet unknown type
I have a templated wrapper function that calls a kernel ( __global__ ) defined
I have a base class with a templated function that has the general templated
Basically, I have a templated class that runs some algorithm, and that algorithm needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.