2012-02-10 44 views
3

在升壓::原手冊中,有::變換< ...>匹配類型爲std終端的語法的例子:我可以知道在變換中匹配boost :: proto :: _的類型嗎?

struct StdComplex 
    : proto::terminal< std::complex<proto::_> > 
{}; 

我想編寫一個轉換,做一些事proto :: _的類型。 例如,當匹配proto :: terminal < std :: complex < T>>時,它會返回boost :: shared_ptr < T>。

這可能嗎?

陳述我的問題的另一種方法是,我如何使下面的代碼段工作?

template<typename T> 
struct Show : proto::callable 
{ 
    typedef T result_type; 

    result_type operator()(T& v) 
    { 
     std::cout << "value = " << v << std::endl; 
     return v; 
    } 
}; 


struct my_grammar 
: proto::when<proto::terminal<proto::_ >, Show<??? what comes here ???>(proto::_value) > 
{}; 

回答

3

您的顯示轉換會比較容易處理,因爲一個多態函數對象:

struct Show : proto::callable 
{ 
    template<class Sig> struct result; 

    template<class This, class T> 
    struct result<This(T)> 
    { 
    typedef T type; 
    }; 

    template<class T> T operator()(T const& v) const 
    { 
     std::cout << "value = " << v << std::endl; 
     return v; 
    } 
}; 

struct my_grammar 
: proto::when<proto::terminal<proto::_ >, Show(proto::_value) > 
{}; 

你對其他問題的回答是:

struct to_shared : proto::callable 
{ 
    template<class Sig> struct result; 

    template<class This, class T> 
    struct result<This(T)> 
    { 
    typedef typename T::value_type base; 
    typedef shared_ptr<base> type; 
    }; 

    template<class T> 
    typename result<to_share(T)>::type operator()(T const& v) const 
    { 
    // stuff 
    } 
}; 


struct my_grammar 
: proto::when<proto::terminal<complex<proto::_> >, to_shared(proto::_value) > 
{}; 
+0

謝謝 - 很優雅! – 2012-02-16 20:23:49

+0

這似乎適用於 proto :: terminal :: type term1 = {{21}}; 但不是 proto :: terminal :: type term1 = {{21}}; 對嗎?在我的應用程序中,我希望終端擁有一個shared_ptr,它指向可以修改的東西。這是一個問題嗎?謝謝。 – 2012-02-25 11:49:29

+0

我從非const int的編譯器中得到的錯誤是: 類型'int'的非常量左值引用無法綁定到'int'類型的臨時值。 – 2012-02-25 11:52:16

相關問題