首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板专门化,使用其他模板

模板专门化,使用其他模板
EN

Stack Overflow用户
提问于 2012-10-17 14:55:39
回答 1查看 215关注 0票数 0

嗯,我有一个问题,我不知道如何正确地说,但我已经包括了下面的代码。我已经从代码中删除了很多,这样就可以更容易地理解问题,但是现在它可能不能100%工作,这只是为了说明。

问题是模板专门化不起作用。

例如:

代码语言:javascript
复制
#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的函数体来解决。

但由于以下原因,我无法做到这一点:

  1. 如果没有为自定义类定义的std::stream操作符<<,则它将无法处理自定义类(但对于空闲函数来说,这是可能的)。
  2. 需要专门化的不仅仅是事件::function函数。但是,事件::序列化和事件::反序列化。

看看第2点;类OStream在模板化成员函数中使用事件::序列化和事件::反序列化。但是事件::序列化和事件::反序列化的专门化必须能够使用OStream (模板)方法。

嗯,我被困在那点上了。我希望有人能给我指明正确的方向!

来源

swift.hpp:

代码语言:javascript
复制
#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_swift

swift/event.hpp

代码语言:javascript
复制
#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_event

swift/OStream.hpp:

代码语言:javascript
复制
#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_OStream

swift/event/primitives.tpl:

代码语言:javascript
复制
#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;
        }
}
}
EN

回答 1

Stack Overflow用户

发布于 2012-10-17 15:13:12

问题是const Int32&不能转换为Int32&。因此,专业化是无法匹配的。将swift/event/primitives.tpl更改为使用此方法将有效:

代码语言:javascript
复制
    template<> String representate<const Int32>(const Int32& value)
    {

或者,您可以将主模板更改为接受const T&而不是T&

对于函数,重载实际上比专门化要好(请参阅Template Specialization VS Function Overloading):

代码语言:javascript
复制
    String representate(Int32 value)
    {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12937188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档