1

给定一组函数,例如:

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);

是否可以采用某种变体类型的数组并根据其内容触发正确的功能?我的应用程序是我想将一组参数 X 传递给另一个进程,在那里我只能选择传递单个指针。我的想法是发送一个指向 std::vector<boost::any> 的指针,然后根据其内容以某种方式计算出上述哪些方法可以触发。

这涉及我对跨线程事件和通信的实验,因此它可能看起来不必要的深奥!

编辑:好的,例如,这是意图。显然它不会编译(模板解析发生在编译时,但我想确定在运行时调用哪个函数!):



#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
    }
}

4

2 回答 2

0

好的,我在这方面取得了一些进展。如果我使用 boost::any 数组,我可以在 void * 之间进行转换(并因此将其作为自定义窗口消息中的 lParam 传递给 msgProc)。解决方案是发送者和接收者类是否具有相同的模板参数。也就是说,是这样的(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();
}

上面演示了从 boost::any 的向量到 void *,然后返回到 boost::any 的向量,在具有正确数量和类型的某个对象上调用函数。

于 2010-09-12T12:38:49.637 回答
0

是的,boost::variant知道它当前存储的值类型。它允许访问并调用正确的重载operator()boost::any使用一种根本不同的技术,无法告诉您它当前存储的内容。

于 2010-09-11T23:01:25.677 回答