嗯,我有一个问题,我不知道如何正确地说,但我已经包括了下面的代码。我已经从代码中删除了很多,这样就可以更容易地理解问题,但是现在它可能不能100%工作,这只是为了说明。
问题是模板专门化不起作用。
例如:
#include <iostream>
#include <swift.hpp>
#include <swift/event.hpp>
#include <swift/event/primitives.tpl>
using namespace swift;
template<class T> void print(const T& value)
{
std::cout << event::representate(value) << std::endl;
}
int main()
{
Int32 x = 23;
print(x);
}问题是,"~“将被打印出来。所以,事件:在这个例子中,points ()指向在“ES-10/Event.hpp”中定义的函数,而不是指向在“ES-10/event/基元tives.hpp”中定义的专门函数。
好的,这并不是真正的问题,因为它可以很容易地解决,因为它可以很容易地通过将“could /swift. the”中的表示函数更改为专门的reprensentate的函数体来解决。
但由于以下原因,我无法做到这一点:
看看第2点;类OStream在模板化成员函数中使用事件::序列化和事件::反序列化。但是事件::序列化和事件::反序列化的专门化必须能够使用OStream (模板)方法。
嗯,我被困在那点上了。我希望有人能给我指明正确的方向!
来源
swift.hpp:
#ifndef HG_swift
#define HG_swift
// C++ Includes
#include <boost/cstdint.hpp>
#include <string>
namespace swift
{
// Cross-platform primairy types:
typedef void Void;
typedef bool Bool;
typedef Bool Bit;
typedef char Char;
typedef unsigned char UChar;
typedef uint8_t Byte;
typedef int16_t Int16;
typedef uint16_t UInt16;
typedef int32_t Int32;
typedef uint32_t UInt32;
typedef Int16 Short;
typedef UInt16 UShort;
typedef Int32 Int;
typedef UInt32 UInt;
typedef double Double;
typedef float Float;
typedef std::string String;
}
#endif //HG_swiftswift/event.hpp
#ifndef swift_event
#define swift_event
#include <iostream>
#include <sstream>
#include <exception>
#include <swift/String.hpp>
namespace swift
{
class OStream;
class IStream;
namespace event
{
template<class T> String representate(T& value)
{
return "~";
}
template<class T> void serialize(T& value, OStream& stream) { }
template<class T> void deserialize(T& value, IStream& stream) { }
}
}
#endif //swift_eventswift/OStream.hpp:
#ifndef HG_swift_OStream
#define HG_swift_OStream
// Imports:
#include "swift.hpp"
// C++ includes:
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
namespace swift
{
struct OStream
{
boost::archive::binary_oarchive archive;
OStream(std::ostream& os) : archive(os, boost::archive::no_header) {}
template<class T> void write(T value)
{
event::serialize(value, *this);
}
};
}
#endif //HG_swift_io_OStreamswift/event/primitives.tpl:
#include <swift.hpp>
#include <swift/event.hpp>
//#include <swift/IStream.hpp>
#include <swift/OStream.hpp>
#include <iostream>
#include <sstream>
namespace swift
{
// Events
namespace event
{
// Events for primary types:
// Int32
template<> String representate<Int32>(Int32& value)
{
std::stringstream str;
str << value;
return str.str();
}
template<> void serialize<Int32>(Int32& value, OStream& stream)
{
stream.archive & value;
}
template<> void deserialize<Int32>(Int32& value, IStream& stream)
{
stream.archive & value;
}
}
}发布于 2012-10-17 15:13:12
问题是const Int32&不能转换为Int32&。因此,专业化是无法匹配的。将swift/event/primitives.tpl更改为使用此方法将有效:
template<> String representate<const Int32>(const Int32& value)
{或者,您可以将主模板更改为接受const T&而不是T&。
对于函数,重载实际上比专门化要好(请参阅Template Specialization VS Function Overloading):
String representate(Int32 value)
{https://stackoverflow.com/questions/12937188
复制相似问题