IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Surchargé une fonction template avec template


Sujet :

Langage C++

  1. #1
    Membre éclairé Avatar de SKone
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2004
    Messages
    333
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Canada

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2004
    Messages : 333
    Par défaut Surchargé une fonction template avec template
    Bonjour,

    Je sais que ce problème a déjà été présenté dans ce forum mais les solutions proposées de fonctionne pas.
    Dans mon cas j'ai un membre dans le fichier JSONSerializer.h:
    Code Cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    #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"
    	}
    }

    Dans le fichier JSONSerializer.inl:
    Code Cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    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();
    }
     
    template <>
    inline
    UInt64			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, UInt64 const )
    {
    	return oValue.GetUint64();
    }
     
    template <>
    inline
    Int8			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Int8 const )
    {
    	return static_cast< Int8 >( oValue.GetInt() );
    }
     
    template <>
    inline
    Int16			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Int16 const )
    {
    	return static_cast< Int16 >( oValue.GetInt() );
    }
     
    template <>
    inline
    Int32			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Int32 const )
    {
    	return oValue.GetInt();
    }
     
    template <>
    inline
    Int64			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Int64 const )
    {
    	return oValue.GetInt64();
    }
     
    template <>
    inline
    f32			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, f32 const )
    {
    	return static_cast< f32 >( oValue.GetDouble() );
    }
     
    template <>
    inline
    f64			JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, f64 const )
    {
    	return oValue.GetDouble();
    }
     
    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() ) );
    }
     
    template <>
    inline
    Core::UTF32String	JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Core::UTF32String const )
    {
    	return Core::StringTool::UTF8StringToUTF32StringCopy( Core::UTF8String( oValue.GetString() ) );
    }
     
    template <>
    inline
    Core::WideString	JSONSerializer::GetObjectFromValue( rapidjson::Value const& oValue, Core::WideString const )
    {
    	return Core::StringTool::UTF8StringToWideStringCopy( Core::UTF8String( oValue.GetString() ) );
    }

    Je voudrais surchargé GetObjectFromValue pour template < typename ScalarType > Math::tVector4< ScalarType >;
    Donc je vais dans Vector4.h et je procède de la manière suivante dans Math/Vector4.h:
    Code Cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    #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;
    		}
    	}
    }

    Auriez-vous une idée de ce que j'ai mal fait ?

    Merci

    PS: Compilateur Intel C++ 14

  2. #2
    Membre Expert

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2004
    Messages
    1 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 1 391
    Par défaut
    Ça ne marche pas parce que ta fonction est membre, donc tu ne peux pas surcharger la fonction à l'extérieur. Je vois que cette fonction est statique, tu ne peux pas la passer en fonction libre ? Ce qui te permettra de la surcharger.

  3. #3
    Membre éclairé Avatar de SKone
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2004
    Messages
    333
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Canada

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2004
    Messages : 333
    Par défaut
    En effet, je connaissais pas cette contrainte.
    Comme ma classe contient que des fonctions static je transforme le tout en Namespace et ça fix mon problème de cette manière je me garde le droit de faire Core::JSONSerializer::GetObjectFromValue...

    Merci => Resolu

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [1.x] appelle d'une fonction dans le template
    Par scarabe088 dans le forum Symfony
    Réponses: 1
    Dernier message: 13/09/2011, 21h56
  2. Stocker la signature d'une fonction dans un template!
    Par nixmind dans le forum C++/CLI
    Réponses: 4
    Dernier message: 20/06/2011, 20h23
  3. [Smarty] Utilisation d'une fonction dans un template
    Par HwRZxLc4 dans le forum Bibliothèques et frameworks
    Réponses: 0
    Dernier message: 15/07/2010, 21h57
  4. Creation d'une fonction temporaire avec droit datareader
    Par Bjuice2 dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 26/10/2004, 14h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo