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

  • Home
  • SEARCH
  • 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 1037691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:52:54+00:00 2026-05-16T14:52:54+00:00

I have to store a list of different boost::function objects. To provide this I’m

  • 0

I have to store a list of different boost::function objects. To provide this I’m using boost::any. I have a few functions which takes different functions signatures, pack them into any and then insert into special map with given type. Here is the code:

enum TypeEnumerator
{
    e_int,
    e_float,
    e_double
};

typedef map< string, pair<any, TypeEnumerator> > CallbackType;
CallbackType mCallbacks;

void Foo(const string &name, function<float ()> f)
{
    mCallbacks[name] = make_pair(any(f), CLASS::e_float);
}
void Foo(const string &name, function<int ()> f) { /* the same, but with e_int */ }
void Foo(const string &name, function<double ()> f) { /* the same, but with e_double */ }

Now I have in map boost function, packed into any with given type from enum, to recognize it in future. Now I have to call given functions. The casting from any won’t work:

BOOST_FOREACH(CallbackType::value_type &row, mCallbacks)
{
    // pair<any, TypeEnumerator>
    switch (row.second.second) // Swith the TypeEnumerator
    {
        case 0: // int
            any_cast< function<int ()> >(row.first)();
        break;
        case 1: // float
            any_cast< function<float ()> >(row.first)();
        break;
        case 2: // double
            any_cast< function<double ()> >(row.first)();
        break;
    }
}

This won’t cast and during running I get the exception:

  what():  boost::bad_any_cast: failed conversion using boost::any_cast

Is it possible to convert back the boost::function object?

  • 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-16T14:52:54+00:00Added an answer on May 16, 2026 at 2:52 pm

    @TC provided the solution for the runtime error. But I believe you should use Boost.Variant instead of Boost.Any as there are only a fixed selection of types it can store. With Boost.Variant you could eliminate that enum too, as it already provided a standard visitor pattern interface. (result):

    #include <boost/variant.hpp>
    #include <boost/function.hpp>
    #include <boost/foreach.hpp>
    #include <map>
    #include <string>
    #include <iostream>
    
    typedef boost::variant<boost::function<int()>,
                           boost::function<float()>,
                           boost::function<double()> > Callback;
    typedef std::map<std::string, Callback> CallbackType;
    
    CallbackType mCallbacks;
    
    void Foo(const std::string& name, const Callback& f) {
        mCallbacks[name] = f;
    }
    
    //------------------------------------------------------------------------------
    
    float f() { 
        std::cout << "f called" << std::endl;
        return 4;
    }
    
    int g() {
        std::cout << "g called" << std::endl;
        return 5;
    }
    
    double h() {
        std::cout << "h called" << std::endl;
        return 4;
    }
    
    //------------------------------------------------------------------------------
    
    struct call_visitor : public boost::static_visitor<> {
        template <typename T>
        void operator() (const T& operand) const {
            operand();
        }
    };
    
    
    int main () {
        Foo("f", boost::function<float()>( f ));
        Foo("g", boost::function<int()>( g ));
        Foo("h", boost::function<double()>( h ));
    
        BOOST_FOREACH(CallbackType::value_type &row, mCallbacks) {
            boost::apply_visitor(call_visitor(), row.second);
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.