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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:57:15+00:00 2026-05-26T09:57:15+00:00

I want to use boost::any to store heterogeneous function pointers. I get an exception

  • 0

I want to use boost::any to store heterogeneous function pointers. I get an exception when I try to use boost::any_cast to recast to the function pointer.

Is what I want to do even allowed?

.h:

typedef void(*voidFunction)(void);
struct functionInfo{
       CString    functionName;
       boost::any functionPointer;
};
void foo();
int foo2(int a);

.cpp

void foo()
{
  ;//do something
}

int foo2(int a)
{
  ;//do something
}

void main()
{
    vector<functionInfo> functionList;
    functionInfo myInfo;
    myInfo.functionName = _T("foo");
    myInfo.functionPointer = &foo;
    functionList.push_back(myInfo);
    myInfo.functionName = _T("foo2");
    myInfo.functionPointer = &foo2;
    functionList.push_back(myInfo);

    voidFunction myVoidFunction = boost::any_cast<voidFunction>(functionList[0].functionPointer);

}

—-EDIT—-

Ok, you are right, the reason why it acted like this is because foo is a class member function.

Meaning:

void MyClass::foo();

myInfo.functionPointer = &MyClass::foo;

so I needed to typedef:

typedef void(MyClass::*voidClassFunction)(void);
voidClassFunction myVoidFunction = boost::any_cast<voidClassFunction>(functionList[0].functionPointer);
  • 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-26T09:57:15+00:00Added an answer on May 26, 2026 at 9:57 am

    Is what I want to do even allowed?

    Absolutely. As long as you cast it back to exactly the type you gave it.

    And that’s your problem. You don’t. foo2 is not a voidFunction. Therefore, you cannot cast it to one.

    The purpose of boost::any is to have a void* that is guaranteed to either work correctly according to the C++ standard or throw an exception. The C++ standard allows the conversion of any (non-member) pointer type to a void*. It also allows conversion of a void* back to a type, provided that the type being provided is exactly the same type as the original. If it’s not, welcome to undefined behavior.

    boost::any exist to prevent undefined behavior by storing type information with the void*. It will rightly throw an exception when you attempt to cast something to the wrong type. As you do here. boost::any is not a way to pretend that types don’t exist and to pretend that you can turn anything into something else. It’s just a type-safe typeless container. You still need to know what you actually put there.

    There is no way to store a list of functions with arbitrary argument lists and call them with the same argument list. The user must provide a function or functor with the right argument list that you expect. boost::bind is a way to adapt a function/functor for a particular argument list, but the user must explicitly use it.

    The best you can do is have a list of specific function parameter sets that you accept, stored in a boost::variant object. You can use a visitor to figure out which particular function to call.

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

Sidebar

Related Questions

I want to use boost::crc so that it works exactly like PHP's crc32() function.
Ok, first of all I don't want to use Boost, or any external libraries.
I attempted to use boost::function<x> wrapper to store and convert my functional objects and
I'm trying to use boost::bind with the std::sort function. I want to bind sort
I am using boost build in my project and now i want to use
I want to use the Publish.GacRemove function to remove an assembly from GAC. However,
I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and
I`m writing a web spider and want to use boost regex library instead of
Say want to store the following: typedef std::function<void(int)> MyFunctionDecl; ..in a collection: typedef std::vector<MyFunctionDecl>
I want to use Boost.Filesystem library to manipulate paths, files and directories. My question

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.