簡體   English   中英

提升融合奇數

[英]Boost fusion oddity

我正在嘗試Fusion,發現很奇怪的東西...這是代碼...我用// ############突出顯示了有問題的代碼######

#include <tr1/cstdint>
#include <tr1/functional>
#include <string>
#include <iostream>

//  #define FUSION_MAX_VECTOR_SIZE 64

#define BOOST_MPL_LIMIT_STRING_SIZE 128

#include <boost/type_traits.hpp>
#include <boost/mpl/string.hpp>
#include <boost/fusion/algorithm.hpp>
#include <boost/fusion/tuple.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/container/generation.hpp>
#include <boost/fusion/container/generation/vector_tie.hpp>

typedef std::tr1::int32_t int32;

typedef std::tr1::int64_t int64;

template < class type_const_ref >
struct remove_const_reference
{
    typedef typename boost::remove_reference < type_const_ref >::type type_const;
    typedef typename boost::remove_const < type_const >::type type;
};

template < class T >
class MetaClass;

namespace fusion = boost::fusion;

template < class T >
struct ConstRefFieldMap
{
    typedef typename MetaClass < T >::FieldNames FieldNames;
    typedef typename MetaClass < T >::ConstRefFields ConstRefFields;
    typedef typename boost::fusion::result_of::zip < FieldNames const, ConstRefFields const >::type type;
};

template < class T >
static typename MetaClass < T >::FieldNames fieldNames()
{
    return typename MetaClass < T >::FieldNames();
}

template < class T >
static typename MetaClass < T >::ConstRefFields constRefFields(T const &obj)
{
    return MetaClass < T >::constRefFields(obj);
}

template < class T >
static typename ConstRefFieldMap < T >::type const constRefFieldMap(T const &obj)
{
    return boost::fusion::zip(fieldNames < T >(), constRefFields(obj));
}

class Currency
{
    private:
        typedef MetaClass < Currency > Meta;

        friend class MetaClass < Currency >;

    private:
        std::string m_isoCode;

        int32 m_rank;

    public:
        Currency(std::string const &isoCode, int32 const rank)
        : m_isoCode(isoCode)
        , m_rank(rank)
        {
        }

        std::string const& getIsoCode() const
        {
            return m_isoCode;
        }

        int32 const getRank() const
        {
            return m_rank;
        }

    private:
        void setIsoCode(std::string const &isoCode)
        {
            m_isoCode = isoCode;
        }

    public:
        void setRank(int32 rank)
        {
            m_rank = rank;
        }
};

template <>
class MetaClass < Currency >
{
    public:
        typedef Currency data_type;

    public:
        typedef std::string IsoCodeType;

        typedef int32 RankType;

        typedef boost::fusion::vector <
            boost::mpl::string < 'i', 's', 'o', 'C', 'o', 'd', 'e' >
        ,   boost::mpl::string < 'r', 'a', 'n', 'k' >
        > FieldNames;

        typedef boost::fusion::vector <
            IsoCodeType &
        ,   RankType &
        > MutableRefFields;

        typedef boost::fusion::vector <
            IsoCodeType const &
        ,   RankType const &
        > ConstRefFields;

        static MutableRefFields mutableRefFields(Currency &obj)
        {
            return MutableRefFields(obj.m_isoCode, obj.m_rank);
        }

        static ConstRefFields constRefFields(Currency const &obj)
        {
            return ConstRefFields(obj.m_isoCode, obj.m_rank);
        }

};

template < class T, class U >
static typename ConstRefFieldMap < T >::type const constRefFieldMapTest(T const &obj, U const &u)
{
    return boost::fusion::zip(fieldNames < T >(), u);
}

int main()
{
    Currency const EUR("EUR", 500);
    using boost::fusion::any;

    {
        std::cout << boost::fusion::at_c < 0 >(constRefFields(EUR)) << " : " << boost::fusion::at_c < 1 >(constRefFields(EUR)) << std::endl;
        ConstRefFieldMap < Currency >::type const &fm = boost::fusion::zip(fieldNames < Currency >(), constRefFields(EUR));
// ############ TROUBLE HERE ######
//        ConstRefFieldMap < Currency >::type const &fm = constRefFieldMap(EUR);
// ############ TROUBLE HERE ######
        {
            {
                typedef boost::fusion::result_of::at_c < ConstRefFieldMap < Currency >::type, 0 >::type field_value_type;
                field_value_type const v = boost::fusion::at_c < 0 >(fm);
                typedef boost::fusion::result_of::at_c < field_value_type, 0 >::type field_name_type;
                field_name_type const n = boost::fusion::at_c < 0 >(v);
                typedef boost::fusion::result_of::at_c < field_value_type, 1 >::type field_data_type;
                field_data_type const d = boost::fusion::at_c < 1 >(v);
                std::cout << boost::mpl::c_str < remove_const_reference < field_name_type >::type >::value << " : " << d << std::endl;
            }

            {
                typedef boost::fusion::result_of::at_c < ConstRefFieldMap < Currency >::type, 1 >::type field_value_type;
                field_value_type const v = boost::fusion::at_c < 1 >(fm);
                typedef boost::fusion::result_of::at_c < field_value_type, 0 >::type field_name_type;
                field_name_type const n = boost::fusion::at_c < 0 >(v);
                typedef boost::fusion::result_of::at_c < field_value_type, 1 >::type field_data_type;
                field_data_type const d = boost::fusion::at_c < 1 >(v);
                std::cout << boost::mpl::c_str < remove_const_reference < field_name_type >::type >::value << " : " << d << std::endl;
            }
        }
    }
}

如果使用constRefFieldMap()函數,則會得到垃圾值或SIGSEGV。 如果我直接調用boost :: fusion :: zip,它會完美地工作。 這是輸出...

EUR : 500
isoCode : EUR
rank : 500

我之前看過這個問題 ...我在這里遇到同樣的問題嗎???

編輯1:

舉例說明我要做什么...

實際上...我正在嘗試編寫這樣的代碼。

MetaObject < Currency const > EUR_META(make_meta_object(EUR));
std::cout  << get_field < std::string >("isoCode", EUR_META.constRefFieldMap()) << std::endl;

MetaObject < Currency > GBP_META(make_meta_object(GBP));
MutableRefFieldMap < Currency >::type const &fm = GBP_META.mutableRefFieldMap();
std::cout  << set_field("rank", fm, 497) << std::endl;

我可以通過字段名稱調用的訪問器和修飾符...

我計划在我的代碼生成器的幫助下,編寫一個精神分析器來解析JSON和XML並創建對象。 主要思想是避免為每個對象生成解析代碼,而僅為已使用的那些對象生成解析代碼,從而減小二進制大小。 我現在有數千個物體。

我現在已經開始工作了。

不幸的是,這個問題並沒有引起更多的關注,但是我想大量的代碼並沒有太大幫助(而且模板元編程並不那么受歡迎)。

無論如何,您是對的,這是一個類似的問題。

問題在於融合中的視圖不會復制參數,它們只會保留對其的引用。 這使您可以通過中間變量來修改原始參數。

問題在於,在C ++中,您被授權將臨時綁定到const引用,並且臨時生存期延長到引用的生存期。 但是,此行為不是可傳遞的,從而導致了很多麻煩。 (Clang會嘗試診斷那些情況,不幸的是第一個補丁失敗了:p)

因此,您的問題在一行中:

return boost::fusion::zip(fieldNames < T >(), constRefFields(obj));
  • fieldNames<T>()創建臨時,綁定到一個const參考,它的壽命被延長,直到表達式的結尾: ;
  • 當您返回視圖時,臨時生存期已到期,因此您將保持懸掛的引用

快速修復:使fieldNames<T>()具有局部靜態變量並返回對此變量的引用,這將解決生命周期問題。

我仍然不了解您的嘗試,因此我無法真正給出“更明智的”建議:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM