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 1008253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:44:37+00:00 2026-05-16T08:44:37+00:00

Given the following template: template<class T> class Container { private: boost::function<T> f; }; …

  • 0

Given the following template:

template<class T>
class Container
{
private:

    boost::function<T> f;
};

… and its instantiation, perhaps as follows:


    Container<bool(int, int)> myContainer;

, is there a way to access the return type of the function description and compile conditionally against it? For example, if the caller specifies his function returns bool (as in the above case), I want to include a function that returns a value. If he specifies that the function is void, I don’t want this function to be included. For example:


// Include if the return type of T is void
template<class T1, class T2>
void DoSomething(T1 t1, T2 t2)
{
    f(t1, t2);
}

// Include if the return type of T is not void
template<class T1, class T2>
***whatever the return type is*** DoSomething(T1 t1, T2 t2)
{
    return f(t1, t2);
}

I’m guessing there is a solution here, but it probably involves some horrendously obfuscated template meta-programming solution. I know Gregor Cantor went mad contemplating infinity… template meta-programming kind-of has the same effect on me :p.

Thanks for any thoughts you might have.

RobinsonT

Edit: Obviously this can be solved by implementing a different class (perhaps derived from a common base), one called VoidContainer and the other called ReturnsContainer (or similar). However this seems a little unsatisfactory to me…

  • 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-05-16T08:44:37+00:00Added an answer on May 16, 2026 at 8:44 am

    I don’t think you actually need to specialize for void return type. A void function is allowed to return the “result” of another void function for exactly this scenario.

    void foo() { }
    void bar() { return foo(); } //this is OK
    
    int main()
    {
        bar();
    }
    

    So your only problem would be how to determine the return type.

    It appears that boost::function has a typedef for result_type (see http://beta.boost.org/doc/libs/1_37_0/doc/html/boost/functionN.html)

    #include <boost/function.hpp>
    
    
    template<class T>
    class Container
    {
    public:
        typedef typename boost::function<T>::result_type result_type;
    private:
    
        boost::function<T> f;
    };
    
    Container<bool(int, int)>::result_type r = true;
    

    Edit:
    Now that you know what the result_type is, and you do need to distinguish between void/non-void results, you can employ enable_if and disable_if. The only complication is that those only work with function templates, so a non-template foo calls a templated do_foo.

    #include <boost/function.hpp>
    #include <boost/utility/enable_if.hpp>
    #include <boost/type_traits.hpp>
    #include <cstdio>
    
    template<class T>
    class Container
    {
    public:
        typedef typename boost::function<T>::result_type result_type;
    
    
        result_type foo() 
        {
            return do_foo<result_type>();
            //note that this still works because you can return the void result! :)
        }
    private:
        //use this if the result_type is void
        template <class U>
        typename boost::enable_if<boost::is_same<U, void>, U >::type do_foo()
        {
            std::puts("for void");
        }
    
        //else
        template <class U>
        typename boost::disable_if<boost::is_same<U, void>, U>::type do_foo()
        {
            std::puts("other");
            return U();
        }
    private:
    
        boost::function<T> f;
    };
    
    
    int main()
    {
        Container<void()> a;
        a.foo();
    
        Container<int()> b;
        b.foo();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's an article that will help. It's a bit more… May 16, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer Bootstrap class should extends the Bootstrap Abstract class. class Bootstrap… May 16, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer Also, it seems as if the dir you are checking… May 16, 2026 at 1:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Given the following template: template <typename T> class wrapper : public T {}; What
Given the following snippet: #include <stdio.h> typedef signed long long int64; typedef signed int
I have a template class that contains a std::map that stores pointers to T
When using a container class like vector , list , etc., I can use
From cplusplus.com: template < class Key, class Compare = less<Key>, class Allocator = allocator<Key>
Given the following InsertItemTemplate (simplified) I'm not getting anything back in the event object's
Given the following code: a = 0 def foo(): # global a a +=
Given the following indexes for an Oracle database: CREATE INDEX subject_x1 ON subject (code);
Given the following: module MyModule = let myObj = new MyObj() type MyType() =
Following techniques from 'Modern C++ Design', I am implementing a persistence library with various

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.