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

Related Questions

Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
its templates again ;-) Given the following template member functions and CRTP class: template<typename
given the following template function : template <class T> void DoSomething(T &obj1, T &obj2)
Given the following template: template <typename T> class wrapper : public T {}; What
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
Given the following code: void f() { class A { template <typename T> void
I have the following template class and a (global) variable of its type: template
Why does the following give no compilation error?: // T.h template<class T> class X
Why does the following code give me an error (g++ 4.1.2)? template<class A> class
I have a problem with nested templates and their template specialization. Given the following

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.