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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:50:27+00:00 2026-05-16T18:50:27+00:00

Given a set of functions, such as: template<class A1> Void Go(A1 a); template<class A1,

  • 0

Given a set of functions, such as:

template<class A1>
Void Go(A1 a);

template<class A1, class A2> Void Go(A1 a1, A2 a2);

template<class A1, class A2, class A3> Void Go(A1 a1, A2 a2, A3 a3);


Is it possible to take an array of some variant type and given its contents, fire the correct function? My application for this is that I want to pass a set of parameters X, to another process, where I only have the option of passing a single pointer. My idea was to send a pointer to a std::vector<boost::any> and then to somehow work out which of the above methods to fire given its contents.

This concerns my experiments with cross-thread eventing and communication, hence it may seem unnecessarily esoteric!

Edit: ok, for example, this is the intention. Obviously it doesn’t compile (the template resolution occurs at compile-time, but I want to determine which function to call at run-time!):



#include <boost\any.hpp>

#include <vector>
#include <iostream>
#include <string>


class A
{

public:

    void Go()
    {
        std::cout << L"(0 params)\n";
    }

    template
    void Go(U0 u0)
    {
        std::cout << L"1 param " << u0 << L"\n";  
    }

    template
    void Go(U0 u0, U1 u1)
    {
        std::cout << L"2 params " << u0 << L" " << u1 << L"\n";   
    }

    template
    void Go(U0 u0, U1 u1, U2 u2)
    {
        std::cout << L"3 params " << u0 << L" " << u1 << L" " << u2 << L"\n"; 
    }

};

class B
{

public:

    void Whatever() {}

};

int main(int argc, wchar_t* argv[])
{
    // Create a collection of variants.

    std::vector<boost::any> myVariants;

    B myB;

    myVariants.push_back(123);
    myVariants.push_back(std::wstring(L"Test"));
    myVariants.push_back(&myB);



    // Take a void pointer to them.

    void *variants = &myVariants;



    // Convert back into an array.

    std::vector<boost::any>& myConverted = *(std::vector<boost::any> *)(variants);



    // Fire the correct event on A.

    A myA;

    switch(myConverted.size())
    {
    case 0:
        myA.Go();
        break;

    case 1:
        myA.Go(myConverted[0]);
        break;

    case 2:
        myA.Go(myConverted[0], myConverted[1]);
        break;

    case 3:
        myA.Go(myConverted[0], myConverted[1], myConverted[2]);
        break;

    default: ;
        // throw
    }
}

  • 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-16T18:50:28+00:00Added an answer on May 16, 2026 at 6:50 pm

    Ok, I made some progress with this. If I use an array of boost::any, I can convert to and from a void * (and hence pass it as an lParam in a custom window message to a msgProc). The solution is if both sender and receiver classes have the same template parameters. That is to say, something like this (should compile as a console project in 2010):

    
    
    #include <boost\any.hpp>
    
    #include <vector>
    #include <iostream>
    #include <string>
    
    // A class to receive the event.
    
    template<typename A0 = int, typename A1 = int, typename A2 = int>
    class A
    {
    
    public:
    
        void Go()
        {
            std::wcout << L"(0 params)\n";
        }
    
        void Go(A0 u0)
        {
            std::wcout << L"1 param " << u0 << L"\n"; 
        }
    
        void Go(A0 u0, A1 u1)
        {
            std::wcout << L"2 params " << u0 << L" " << u1 << L"\n";  
        }
    
        void Go(A0 u0, A1 u1, A2 u2)
        {
            std::wcout << L"3 params " << u0 << L" " << u1 << L" " << u2 << L"\n";    
        }
    
    };
    
    // A class to demonstrate passing an abitrary object.
    
    class B
    {
    
    public:
    
    
    };
    
    // Implement operator on type B so we can use std::cout.
    
    std::wostream& operator << (std::wostream& o, const B& b)
    {
        o << L"Hello!";
    
        return o; 
    }
    
    // A class that converts an array of boost::any from void and calls an appropriate function on A.
    
    template<typename A0 = int, typename A1 = int, typename A2 = int>
    class C
    {
    
    public:
    
        void Everything()
        {
    
            // Create a collection of variants.
    
            std::vector<boost::any> myVariants;
    
            B myB;
    
            myVariants.push_back(123);
            myVariants.push_back(myB);
    
    
    
            // Take a void pointer to them.
    
            void *variants = &myVariants;
    
    
    
            // Convert back into an array.
    
            std::vector<boost::any>& myConverted = *(std::vector<boost::any> *)(variants);
    
    
    
            // Fire the correct event on A.
    
            A<A0, A1, A2> myA;
    
            switch(myConverted.size())
            {
            case 0:
                myA.Go();
                break;
    
            case 1:
                myA.Go(boost::any_cast<A0>(myConverted[0]));
                break;
    
            case 2:
                myA.Go(boost::any_cast<A0>(myConverted[0]), boost::any_cast<A1>(myConverted[1]));
                break;
    
            case 3:
                myA.Go(boost::any_cast<A0>(myConverted[0]), boost::any_cast<A1>(myConverted[1]), boost::any_cast<A2>(myConverted[2]));
                break;
    
            default: ;
                // throw
            }
        }
    };
    
    int main(int argc, wchar_t* argv[])
    {
        C<int, B> c;
    
        c.Everything();
    }
    

    The above demonstrates going from a vector of boost::any to a void * and then back to a vector of boost::any, calling a function on some object with the correct arity and types.

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