使用模板类覆盖模板成员

时间:2014-10-11 21:36:42

标签: c++ templates override template-specialization

我的问题与此非常相似: Template specialisation where templated type is also a template但有多个文件。

在JSONSerializer.h上:

#pragma once

#include "Kernel/Kernel.h"

#include "Core/String/String.h"
#include "Core/String/StringTool.h"
#include "Core/Core/Errors.h"

#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

namespace eMV
{
    typedef rapidjson::UTF8<>                                                       RJ_UTF8Trait;
    typedef rapidjson::GenericStringBuffer< rapidjson::UTF8<> >                     StringBuffer;
    typedef rapidjson::Writer< StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<> > Writer;
    typedef rapidjson::GenericReader< rapidjson::UTF8<>, rapidjson::UTF8<> >        Reader;
    typedef rapidjson::GenericDocument< rapidjson::UTF8<> >                         Document;

    namespace Core
    {
        class JSONSerializer
        {
        public:
            template < typename Type >
            static
            Type                GetObjectFromValue( rapidjson::Value const& oValue, Type const t = Type() );
        };

        #include "Core/Serializer/JSONSerializer.inl"
    }
}

JSONSerializer.inl:

template < typename Type >
inline
Type            JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Type const )
{
    EMV_NOT_IMPL;
}

template <>
inline
UInt8           JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, UInt8 const )
{
    return static_cast< UInt8 >( oValue.GetUint() );
}

template <>
inline
UInt16          JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, UInt16 const )
{
    return static_cast< UInt16 >( oValue.GetUint() );
}

template <>
inline
UInt32          JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, UInt32 const )
{
    return oValue.GetUint();
}

// etc...

template <>
inline
Core::ASCIIString   JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Core::ASCIIString const )
{
    return Core::StringTool::UTF8StringToASCIIStringCopy( Core::UTF8String( oValue.GetString() ) );
}

//template <>
//Core::UTF8String  JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Core::UTF8String const )
//{
//  //return Core::StringTool::UTF8StringToASCIIStringCopy( Core::UTF8String( oValue.GetString() ) );
//}

template <>
inline
Core::UTF16String   JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Core::UTF16String const )
{
    return Core::StringTool::UTF8StringToUTF16StringCopy( Core::UTF8String( oValue.GetString() ) );
} // etc

这是非常类似的实现,就像这里的解决方案一样:

我想在Vector4.h上专攻:

#pragma once

#include "Math/MathConfig.h"
#include "Math/MathAPI.h"
#include "Math/Consts.h"
#include "Math/Vectorial.h"

#include "Core/Core/TypeTraits.h"
#include "Core/Stream/Stream.h"
#include "Core/Serializer/JSONSerializer.h"
#include "Core/Core/CRC32.h"

namespace eMV
{
    namespace Math
    {
        template < typename ScalarType = Scalar >
        class tVector4
        {
        public:
            // Implementation blabla
        };

// #if USE_VECTOR4_SIMD == 1 && Core::IsSame< f80, Scalar >::Value
// #    error   Vector4 Vectorial & Scalar f80 is not usable
// #endif

// #if Core::IsSame< f80, Scalar >::Value
// #    include "Math/Vector4.hpp"
// #elif    USE_VECTOR4_SIMD
//  // TODO AVX & SSE f64 version with double f64 SSE Instruction OR AVX instruction
//  //      => TODO Only the Dot
// #    include "Math/Vector4Vectorial.hpp"
// #else
// #    include "Math/Vector4.hpp"
// #endif
#if Core::IsSame< f80, Scalar >::Value
#   include "Math/Vector4.hpp"
#elif   USE_VECTOR4_SIMD
    // TODO AVX & SSE f64 version with double f64 SSE Instruction OR AVX instruction
    //      => TODO Only the Dot
#   include "Math/Vector4Vectorial.hpp"
#else
#   include "Math/Vector4.hpp"
#endif
    }

    namespace Core
    {
        template < typename ScalarType >
        inline
        Math::tVector4< ScalarType >    JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Math::tVector4< ScalarType > const )
        {
            EMV_ASSERT( oValue.Size() == 4 );

            Math::tVector4< ScalarType >    vVector;

            vVector.Set(    static_cast< ScalarType >( oValue[ 0 ].GetDouble() ),
                            static_cast< ScalarType >( oValue[ 1 ].GetDouble() ),
                            static_cast< ScalarType >( oValue[ 2 ].GetDouble() ),
                            static_cast< ScalarType >( oValue[ 3 ].GetDouble() ) );

            return vVector;
        }
    }
}

我的实现看起来像这里提出的解决方案: Template specialisation where templated type is also a template

但我仍有问题:

2>..\Math/Vector4.h(349): error : declaration is incompatible with function template "Type eMV::Core::JSONSerializer::GetObjectFromValue(const rapidjson::Value &, Type)" (declared at line 766 of "..\Core/Serializer/JSONSerializer.inl")
2>          Math::tVector4< ScalarType >    JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Math::tVector4< ScalarType > const )

PS:Compiler =&gt;英特尔编译器14与C ++ 11。

我做错了什么?

我尝试明确这样的专业化:

template < typename ScalarType >
inline
Math::tVector4< ScalarType >    JSONSerializer::GetObjectFromValue< Math::tVector4< ScalarType > >( rapidjson::Value const& oValue, Math::tVector4< ScalarType > const )

但我的编译器/ C ++不喜欢:

2>..\Math/Vector4.h(349): error : an explicit template argument list is not allowed on this declaration
2>          Math::tVector4< ScalarType >    JSONSerializer::GetObjectFromValue< Math::tVector4< ScalarType > >( rapidjson::Value const& oValue, Math::tVector4< ScalarType > const )

0 个答案:

没有答案