2011-11-29 68 views
1

歌詞:其中Boost :: Variant和function_types:如何將函數放入Boost :: variant?

我嘗試通過MPI實現任務池。所以,我需要某種形式的RPC,而是一個將我的程序的不同部分之間的工作,這意味着處理器A希望處理器B調用函數C與參數D.我們無法通過函數指針進程之間像我們做的線程,因此我們需要一些包裝容器來保存每個流程實例的函數指針。所有內部的源文件\一個程序...所以我開始想知道How to store functional objects with different signature in a container。我的API想法當時是錯誤的 - 最好是在該池構造中定義函數池中的所有函數(至少應該更容易實現)。但是,在實施我面對下一個麻煩:

問題:

這種簡單的代碼(function_typesmpl::vectorvariant):

#include <boost/function_types/function_type.hpp> 
#include <boost/mpl/vector.hpp> 
#include <boost/mpl/vector_c.hpp> 
#include <boost/variant.hpp> 

#include <iostream> 
#include <string> 

template <class T> 
int append(T val) 
{ 
    std::cout << "hello"; 
    return 0; 
} 

int main() 
{ 
    boost::variant<boost::function_types::function_type< boost::mpl::vector<int,int> >::type , boost::function_types::function_type< boost::mpl::vector<int,std::string> >::type > a; 
    return 0; 
} 

不會編譯掉落:

Error 1 error C2066: cast to function type is illegal c:\program files\boost\include\boost\variant\variant.hpp 1231 1 
我們看到:

這個代碼塊:

variant() 
{ 
    // NOTE TO USER : 
    // Compile error from here indicates that the first bound 
    // type is not default-constructible, and so variant cannot 
    // support its own default-construction. 
    // 
    new(storage_.address()) internal_T0(); 
    indicate_which(0); // zero is the index of the first bounded type 
} 

所以我想:如何解決這個錯誤?

我也試過:

#include <boost/function_types/function_type.hpp> 
#include <boost/mpl/vector.hpp> 
#include <boost/mpl/vector_c.hpp> 
#include <boost/variant.hpp> 
#include <boost/function.hpp> 

#include <iostream> 
#include <string> 

template <class T> 
int append(T val) 
{ 
    std::cout << "hello"; 
    return 1; 
} 

int main() 
{ 
    boost::variant< boost::function<int (std::string) >, boost::function<int (int) > > a; 
    a= &append<int>; 

    return 0; 
} 

哪些失敗:

Error 1 error C2668: 'boost::detail::variant::make_initializer_node::apply<BaseIndexPair,Iterator>::initializer_node::initialize' : ambiguous call to overloaded function c:\program files\boost\include\boost\variant\variant.hpp 1330 

如何使boost.variant保持功能的任何想法?

當然,我們可以共享指針發揮仿函數像這樣作者:

#include <boost/variant.hpp> 
#include <boost/shared_ptr.hpp> 

#include <iostream> 
#include <string> 

template <class in, class out> 
struct s_append 
{ 
    out operator()(in val) { 
     std::cout << "hello"; 
     return out(); 
    } 
}; 

int main() 
{ 
    boost::variant<boost::shared_ptr<s_append<int, int> >, boost::shared_ptr< s_append<std::string, int> > > a; 
    boost::shared_ptr<s_append<int, int> > b(new s_append<int, int>); 
    a=b; 
    return 0; 
} 

,它會編譯,但得到的API吮吸 - 你必須1)創建要使用所有功能函子(意爲限有使用當前進程範圍); 2)使用shared_pointers,所以我真的不連得如何調用函數嵌套在該方式(簡單的第一個猜測(*a)(22);只是將不會編譯=(和API開始是那麼糟糕,因爲我們會使用Boost.Any)。

+1

我不太明白在連接到源代碼中的問題,但我想你真正想要的是一些進程間通信。我建議你看看Boost.Interprocess http://www.boost。org/doc/libs/1_48_0/doc/html/interprocess.html – mantler

+0

@manler:進程間跨越一臺機器...... MPI跨越很多=) – myWallJSON

+0

是的,使用'boost:bind()' – karlphillip

回答

0

嘗試插入一個虛擬類型作爲variant的第一個參數。正如您找到的註釋所解釋的,只有變體中的第一個類型用於變體自己的默認構造函數,您可以使用空的結構類型(struct NoFunction {}; )。

這就是說,你可能已經到東西的想法使用boost ::功能的變異類型...他們是默認constructible至少,我不知道什麼其他錯誤你從那個方法中產生了,但只是想要t o如果你不能使用我提到的虛擬類型解決方法,讓你知道你可以追求更多的角度。

相關問題