gpt4 book ai didi

c++ - Boost模板函数多条件

转载 作者:行者123 更新时间:2023-11-28 07:38:47 26 4
gpt4 key购买 nike

我正在尝试通过网络序列化数据,在大多数情况下,模板化正在取得成效。我遇到以下情况时遇到问题。

template < typename type >
class SerializedVector
{
public:
bool SerializeIn( const U8* data, int& bufferOffset );
bool SerializeOut( U8* data, int& bufferOffset ) const;

vector< type > m_data;
};

在基本类型的情况下,序列化只是调用 memcpy(实际上是 htonl)的情况,但对于 std::string,我序列化字节数,然后 memcpy 后面的缓冲区。所以我有一个用于原始类型的模板函数和一个用于 std:string 的特化。很简单。

现在,我想支持类在我的 m_data 成员中进行 self 序列化……像这样:

struct TextEntry
{
bool SerializeIn( const U8* data, int& bufferOffset );
bool SerializeOut( U8* data, int& bufferOffset ) const;

string username;
string message;
};

class PacketTextHistoryResult : public BasePacket
{
public:
PacketTextHistoryResult (){}

bool SerializeIn( const U8* data, int& bufferOffset );
bool SerializeOut( U8* data, int& bufferOffset ) const;

SerializedVector< TextEntry > chat;
};

我已经尝试了很多东西,但这就是我卡住的地方……有更好的主意吗?这是行不通的。

template <typename type>
struct calls_member_serialize : boost::false_type { };

template <>
struct calls_member_serialize< std::string > : boost::false_type { };

template <typename type>
struct calls_member_serialize< boost::is_class< type > > : boost::true_type { };

template < typename type >
bool SerializedVector< type >::SerializeIn( const U8* data, int& bufferOffset )
{
int num = m_data.size();
Serialize::In( data, bufferOffset, num );

struct localScope
{
static void do_work( const U8* data, int& bufferOffset, type temp, boost::true_type const & )
{
temp.SerializeIn( data, bufferOffset ); <<<<<<<< See how I invoke the self-serialization here.
}
static void do_work( const U8* data, int& bufferOffset, type temp, boost::false_type const & )
{
Serialize::In( data, bufferOffset, temp ); // call the standard template function
}
};

for( int i=0; i<num; i++ )
{
type temp;
localScope::do_work( data, bufferOffset, temp, ( calls_member_serialize< type >() ) ); //boost::is_fundamental<type>() || boost::is_class< std::string, type >()
m_data.push_back( temp );
}

return true;
}

最佳答案

我不认为你的第三个calls_member_serialize做你想让它做的事。试试这个:

template <typename type>
struct calls_member_serialize : boost::is_class< type > { };

template <>
struct calls_member_serialize< std::string > : boost::false_type { };

那边calls_member_serialize<int>源自 boost::false_typecalls_member_serialize<TextEntry>源自 boost::true_type .

第二个问题是 struct localScope不是模板类,因此编译器将尝试为每种类型实例化 do_work 函数的两个版本,从而导致编译器错误类型如 std::string .您还需要将 localScope 帮助器类设为模板。但是,模板类不能在函数范围内,因此它看起来像这样(未经测试):

namespace { // put in unnamed namespace to keep it local
template<typename localType>
struct localScope
{
static void do_work( const U8* data, int& bufferOffset, localType temp, boost::true_type const & )
{
temp.SerializeIn( data, bufferOffset );
}
static void do_work( const U8* data, int& bufferOffset, localType temp, boost::false_type const & )
{
Serialize::In( data, bufferOffset, temp ); // call the standard template function
}
};
}

template < typename type >
bool SerializedVector< type >::SerializeIn( const U8* data, int& bufferOffset )
{
int num = m_data.size();
Serialize::In( data, bufferOffset, num );

for( int i=0; i<num; i++ )
{
type temp;
localScope<type>::do_work( data, bufferOffset, temp, ( calls_member_serialize< type >() ) ); //boost::is_fundamental<type>() || boost::is_class< std::string, type >()
m_data.push_back( temp );
}

return true;
}

关于c++ - Boost模板函数多条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16245961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com