Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在.h/.cpp文件中支持不同版本的boost_C++_Boost - Fatal编程技术网

C++ 在.h/.cpp文件中支持不同版本的boost

C++ 在.h/.cpp文件中支持不同版本的boost,c++,boost,C++,Boost,我有一个头文件和.cpp文件(称之为Foo.h/.cpp),其结构如下(为了简单起见,省略了几个不相关的细节): Foo.h #include <boost/bimap.hpp> const std::string & ToHeader(const int msgID); int ToMsgID(const std::string & msgHdr); typedef boost::bimap<int, std::string> MsgIDBimap;

我有一个头文件和.cpp文件(称之为Foo.h/.cpp),其结构如下(为了简单起见,省略了几个不相关的细节):

Foo.h

#include <boost/bimap.hpp>

const std::string & ToHeader(const int msgID);
int ToMsgID(const std::string & msgHdr);

typedef boost::bimap<int, std::string> MsgIDBimap;
MsgIDBimap & GetMessageIDToStringMap();
#include <boost/version.hpp>
#define BOOST_VER_MAJOR (BOOST_VERSION/100000)        // ex.: 1   in  1_37_0
#define BOOST_VER_MINOR ((BOOST_VERSION/100) % 1000)  // ex.: 37  in  1_37_0
#define BOOST_VER_PATCH (BOOST_VERSION % 100)         // ex.: 0   in  1_37_0

/*************************************************************************************************/
#if ((BOOST_VER_MAJOR >= 1) && (BOOST_VER_MINOR >= 35)) // boost greater than or equal to 1.35 (bimap available)
#include <boost/bimap.hpp>
#endif
/*************************************************************************************************/

const std::string & ToHeader(const int msgID);
int ToMsgID(const std::string & msgHdr);

/*************************************************************************************************/
#if ((BOOST_VER_MAJOR >= 1) && (BOOST_VER_MINOR >= 35)) // boost greater than or equal to 1.35 (bimap available)
typedef boost::bimap<int, std::string> MsgIDBimap;
MsgIDBimap & GetMessageIdToStringMap();
#endif
/*************************************************************************************************/
#包括
const std::字符串和ToHeader(const int msgID);
int-ToMsgID(const-std::string和msgHdr);
typedef boost::bimap MsgIDBimap;
MsgIDBimap&GetMessageIDToStringMap();
Foo.cpp

#include "Foo.h"
#include <boost/assign/list_of.hpp>

static MsgIDBimap msgIDBimap = boost::assign::list_of<MsgIDBimap::relation>
    ((int)ROM_MSG, "")
    ... // a whole bunch of other entries
; // end static bimap initialization

const std::string & ToHeader(const int msgID)
{
    MsgIDBimap::left_iterator foundIT = msgIDBimap.left.find(msgID);
    if(foundIT != msgIDBimap.left.end()) // found
    {
        return foundIT->second;
    }

    throw std::string("MSG_ID_NOT_FOUND");
}

int ToMsgID(const std::string & msgHdr)
{
    int msgID = (int)MSG_UNKNOWN; // unknown message header
    MsgIDBimap::right_iterator foundIT = msgIDBimap.right.find(msgHdr);
    if(foundIT != msgIDBimap.right.end()) // found
    {
        msgID = foundIT->second;
    }
    return msgID;
}

MsgIDBimap & GetMessageIdToStringMap()
{
    return msgIDBimap;
}
#include "Foo.h"
#include <boost/assign/list_of.hpp>

#if ((BOOST_VER_MAJOR >= 1) && (BOOST_VER_MINOR >= 35)) // boost greater than or equal to 1.35 (bimap available)

static MsgIDBimap msgIDBimap = boost::assign::list_of<MsgIDBimap::relation>
    ((int)ROM_MSG,            "") // <ROM message> (no header)

; // end static msgIDBimap initialization

const std::string & ToHeader(const int msgID)
{
    MsgIDBimap::left_iterator foundIT = msgIDBimap.left.find(msgID);
    if(foundIT != msgIDBimap.left.end()) // found
    {
        return foundIT->second;
    }

    throw std::string("MSG_ID_NOT_FOUND");
}

int ToMsgID(const std::string & msgHdr)
{
    int msgID = (int)MSG_UNKNOWN; // unknown message header
    MsgIDBimap::right_iterator foundIT = msgIDBimap.right.find(msgHdr);
    if(foundIT != msgIDBimap.right.end()) // found
    {
        msgID = foundIT->second;
    }
    return msgID;
}

MsgIDBimap & GetMessageIdToStringMap()
{
    return msgIDBimap;
}

/*************************************************************************************************/
#else // boost less than 1.35, bimap not available

typedef std::map<int, std::string> MsgIDToStringMap;
static MsgIDToStringMap msgIDToStringMap = boost::assign::map_list_of<int, std::string>
    ((int)ROM_MSG,            "") // <ROM message> (no header)
; // end static msgIDToStringMap initialization

const std::string & ToHeader(const int msgID)
{
    const MsgIDToStringMap::const_iterator foundIT = msgIDToStringMap.find(msgID);
    if(foundIT != msgIDToStringMap.end()) // found
    {
        return foundIT->second;
    }

    throw std::string("MSG_ID_NOT_FOUND");
}

#define FLIP_ARGS(x,y) (y,x)
typedef std::map<std::string, int> StringToMsgIDMap;
static StringToMsgIDMap stringToMsgIDMap = boost::assign::map_list_of<std::string, int>
    FLIP_ARGS((int)ROM_MSG,            "") // <ROM message> (no header)

; // end static stringToMsgIDMap initialization

int ToMsgID(const std::string & msgHdr)
{
    int msgID = (int)MSG_UNKNOWN; // unknown message header
    const StringToMsgIDMap::const_iterator foundIT = stringToMsgIDMap.find(msgHdr);
    if(foundIT != stringToMsgIDMap.end()) // found
    {
        msgID = foundIT->second;
    }
    return msgID;
}
#endif
#包括“Foo.h”
#包括
静态MsgIDBimap MsgIDBimap=boost::assign::列表
((int)ROM_MSG,“”)
... // 一大堆其他的条目
; // 结束静态bimap初始化
const std::string和ToHeader(const int msgID)
{
MsgIDBimap::left_迭代器foundIT=MsgIDBimap.left.find(msgID);
if(foundIT!=msgIDBimap.left.end())//找到
{
返回foundIT->second;
}
抛出std::string(“MSG_ID_NOT_FOUND”);
}
int ToMsgID(const std::string和msgHdr)
{
int msgID=(int)MSG_UNKNOWN;//未知消息头
MsgIDBimap::right\u迭代器foundIT=MsgIDBimap.right.find(msgHdr);
if(foundIT!=msgIDBimap.right.end())//找到
{
msgID=foundIT->second;
}
返回msgID;
}
MsgIDBimap&GetMessageIdToStringMap()
{
返回msgIDBimap;
}
问题是,现在我需要支持一个不支持bimap的早期版本的boost(很糟糕,我知道……但我别无选择)。到目前为止,这是我的解决方案:

Foo.h

#include <boost/bimap.hpp>

const std::string & ToHeader(const int msgID);
int ToMsgID(const std::string & msgHdr);

typedef boost::bimap<int, std::string> MsgIDBimap;
MsgIDBimap & GetMessageIDToStringMap();
#include <boost/version.hpp>
#define BOOST_VER_MAJOR (BOOST_VERSION/100000)        // ex.: 1   in  1_37_0
#define BOOST_VER_MINOR ((BOOST_VERSION/100) % 1000)  // ex.: 37  in  1_37_0
#define BOOST_VER_PATCH (BOOST_VERSION % 100)         // ex.: 0   in  1_37_0

/*************************************************************************************************/
#if ((BOOST_VER_MAJOR >= 1) && (BOOST_VER_MINOR >= 35)) // boost greater than or equal to 1.35 (bimap available)
#include <boost/bimap.hpp>
#endif
/*************************************************************************************************/

const std::string & ToHeader(const int msgID);
int ToMsgID(const std::string & msgHdr);

/*************************************************************************************************/
#if ((BOOST_VER_MAJOR >= 1) && (BOOST_VER_MINOR >= 35)) // boost greater than or equal to 1.35 (bimap available)
typedef boost::bimap<int, std::string> MsgIDBimap;
MsgIDBimap & GetMessageIdToStringMap();
#endif
/*************************************************************************************************/
#包括
#定义BOOST\u VER\u MAJOR(BOOST\u VERSION/100000)//例如:1\u 37\u 0中的1
#定义BOOST\u VER\u MINOR((BOOST\u VERSION/100)%1000)//例如:1\u 37\u 0中的37
#定义BOOST\u VER\u补丁(BOOST\u版本%100)//例如:1\u 37\u 0中的0
/*************************************************************************************************/
#如果((助推大小>=1)和((助推大小>=35))//助推大于或等于1.35(bimap可用)
#包括
#恩迪夫
/*************************************************************************************************/
const std::字符串和ToHeader(const int msgID);
int-ToMsgID(const-std::string和msgHdr);
/*************************************************************************************************/
#如果((助推大小>=1)和((助推大小>=35))//助推大于或等于1.35(bimap可用)
typedef boost::bimap MsgIDBimap;
MsgIDBimap&GetMessageIdToStringMap();
#恩迪夫
/*************************************************************************************************/
Foo.cpp

#include "Foo.h"
#include <boost/assign/list_of.hpp>

static MsgIDBimap msgIDBimap = boost::assign::list_of<MsgIDBimap::relation>
    ((int)ROM_MSG, "")
    ... // a whole bunch of other entries
; // end static bimap initialization

const std::string & ToHeader(const int msgID)
{
    MsgIDBimap::left_iterator foundIT = msgIDBimap.left.find(msgID);
    if(foundIT != msgIDBimap.left.end()) // found
    {
        return foundIT->second;
    }

    throw std::string("MSG_ID_NOT_FOUND");
}

int ToMsgID(const std::string & msgHdr)
{
    int msgID = (int)MSG_UNKNOWN; // unknown message header
    MsgIDBimap::right_iterator foundIT = msgIDBimap.right.find(msgHdr);
    if(foundIT != msgIDBimap.right.end()) // found
    {
        msgID = foundIT->second;
    }
    return msgID;
}

MsgIDBimap & GetMessageIdToStringMap()
{
    return msgIDBimap;
}
#include "Foo.h"
#include <boost/assign/list_of.hpp>

#if ((BOOST_VER_MAJOR >= 1) && (BOOST_VER_MINOR >= 35)) // boost greater than or equal to 1.35 (bimap available)

static MsgIDBimap msgIDBimap = boost::assign::list_of<MsgIDBimap::relation>
    ((int)ROM_MSG,            "") // <ROM message> (no header)

; // end static msgIDBimap initialization

const std::string & ToHeader(const int msgID)
{
    MsgIDBimap::left_iterator foundIT = msgIDBimap.left.find(msgID);
    if(foundIT != msgIDBimap.left.end()) // found
    {
        return foundIT->second;
    }

    throw std::string("MSG_ID_NOT_FOUND");
}

int ToMsgID(const std::string & msgHdr)
{
    int msgID = (int)MSG_UNKNOWN; // unknown message header
    MsgIDBimap::right_iterator foundIT = msgIDBimap.right.find(msgHdr);
    if(foundIT != msgIDBimap.right.end()) // found
    {
        msgID = foundIT->second;
    }
    return msgID;
}

MsgIDBimap & GetMessageIdToStringMap()
{
    return msgIDBimap;
}

/*************************************************************************************************/
#else // boost less than 1.35, bimap not available

typedef std::map<int, std::string> MsgIDToStringMap;
static MsgIDToStringMap msgIDToStringMap = boost::assign::map_list_of<int, std::string>
    ((int)ROM_MSG,            "") // <ROM message> (no header)
; // end static msgIDToStringMap initialization

const std::string & ToHeader(const int msgID)
{
    const MsgIDToStringMap::const_iterator foundIT = msgIDToStringMap.find(msgID);
    if(foundIT != msgIDToStringMap.end()) // found
    {
        return foundIT->second;
    }

    throw std::string("MSG_ID_NOT_FOUND");
}

#define FLIP_ARGS(x,y) (y,x)
typedef std::map<std::string, int> StringToMsgIDMap;
static StringToMsgIDMap stringToMsgIDMap = boost::assign::map_list_of<std::string, int>
    FLIP_ARGS((int)ROM_MSG,            "") // <ROM message> (no header)

; // end static stringToMsgIDMap initialization

int ToMsgID(const std::string & msgHdr)
{
    int msgID = (int)MSG_UNKNOWN; // unknown message header
    const StringToMsgIDMap::const_iterator foundIT = stringToMsgIDMap.find(msgHdr);
    if(foundIT != stringToMsgIDMap.end()) // found
    {
        msgID = foundIT->second;
    }
    return msgID;
}
#endif
#包括“Foo.h”
#包括
#如果((助推大小>=1)和((助推大小>=35))//助推大于或等于1.35(bimap可用)
静态MsgIDBimap MsgIDBimap=boost::assign::列表
((int)ROM_MSG,”)/(无标题)
; // 结束静态msgIDBimap初始化
const std::string和ToHeader(const int msgID)
{
MsgIDBimap::left_迭代器foundIT=MsgIDBimap.left.find(msgID);
if(foundIT!=msgIDBimap.left.end())//找到
{
返回foundIT->second;
}
抛出std::string(“MSG_ID_NOT_FOUND”);
}
int ToMsgID(const std::string和msgHdr)
{
int msgID=(int)MSG_UNKNOWN;//未知消息头
MsgIDBimap::right\u迭代器foundIT=MsgIDBimap.right.find(msgHdr);
if(foundIT!=msgIDBimap.right.end())//找到
{
msgID=foundIT->second;
}
返回msgID;
}
MsgIDBimap&GetMessageIdToStringMap()
{
返回msgIDBimap;
}
/*************************************************************************************************/
#否则//升压小于1.35,bimap不可用
typedef std::map MsgIDToStringMap;
静态MsgIDToStringMap MsgIDToStringMap=boost::assign::map\u列表
((int)ROM_MSG,”)/(无标题)
; // 结束静态msgIDToStringMap初始化
const std::string和ToHeader(const int msgID)
{
const MsgIDToStringMap::const_迭代器foundIT=MsgIDToStringMap.find(msgID);
if(foundIT!=msgIDToStringMap.end())//找到
{
返回foundIT->second;
}
抛出std::string(“MSG_ID_NOT_FOUND”);
}
#定义翻转参数(x,y)(y,x)
typedef std::map StringToMsgIDMap;
静态StringToMsgIDMap StringToMsgIDMap=boost::assign::map\u列表
翻转参数((int)ROM\u MSG,“”/(无标题)
; // 结束静态stringToMsgIDMap初始化
int ToMsgID(const std::string和msgHdr)
{
int msgID=(int)MSG_UNKNOWN;//未知消息头
const StringToMsgIDMap::const_迭代器foundIT=StringToMsgIDMap.find(msgHdr);
if(foundIT!=stringToMsgIDMap.end())//找到
{
msgID=foundIT->second;
}
返回msgID;
}
#恩迪夫
虽然这样做可行,但问题是getMessageIDToString()函数声明/定义只存在于更高版本的boost场景中(这两个场景并非100%一致)。我知道在早期的boost版本场景中永远不需要getMessageIDToString()函数本身

理想情况下,我希望将静态bimap保留在.cpp文件中,并将getMessageIDToString()移动到另一个文件的某个位置,以便Foo.h/.cpp对每个#ifdef块都是100%一致的。问题在于bimap是.cpp文件中的一个静态变量,没有其他文件可以访问它。即使我使bimap成为一个全局变量而不是静态变量,我现在也必须在头文件中公开一个全局变量,这同样只存在于boost的更高版本中


有没有办法解决这个问题,使文件的两个版本(boost>=1.35与早期的boost)始终100%一致,而getMessageIDToString()函数在另一个文件中可用?

回答问题中的注释:

在foo.cpp中:

typedef std::map<int, std::string> MsgIDToStringMap;
//you can remove the static
MsgIDToStringMap msgIDToStringMap = ...

但正如我在第一条评论中所说,我更喜欢单例类。

全局变量不必在头文件中公开。您可以在cpp文件中使用
extern
。我更喜欢使用单例clas