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 :

surchage fonction template


Sujet :

Langage C++

  1. #1
    Membre émérite
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    851
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2010
    Messages : 851
    Points : 2 293
    Points
    2 293
    Par défaut surchage fonction template
    Bonjour,

    Je suis actuellement en train de creer un namespace dans lequel je mets des fonctions templatees qui me permettront de me faciliter la vie dans mes futurs developpements. Cependant voici mon probleme : je souhaiterais creer une fonction qui surchargerait une fonction template pour modifier son comportement lorsque la fonction template recevrait pour template une string. Voici le probleme :

    Code : 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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    namespace General
    {
      template<typename T>
      T getValueFromString(std::string const &s)
      {
        T			var;
        std::istringstream  iss;
     
        std::cout << "in2" << std::endl;
        if (s != "")
          {
    	iss.str(s);
    	iss >> var;
          }
        return var;
      }
     
      template<typename T>
      std::string	getValueFromString(std::string const &s)
      {
        std::string	tmp;
     
        std::cout << "in" << std::endl;
        tmp = s;
        return tmp;
      }
     
      template<typename T>
      T getValueFromString(const char *s)
      {
        std::string	str;
     
        if (s)
          str = s;
        return getValueFromString<T>(str);
      }
     
      template<typename T>
      std::string   toString(T var)
      {
        std::ostringstream  oss;
        std::string     s;
     
        oss << var;
        s = oss.str();
        return s;
      }
     
      template<typename T>
      std::vector<T>  split(const char *str, const char *key)
      {
        std::string     st;
     
        if (!str)
          return split<T>(st, key);
        st = str;
        return split<T>(st, key);
      }
     
      template<typename T>
      std::vector<T>  split(const char *s, std::string const &key)
      {
        return split<T>(s, key.c_str());
      }
     
      template<typename T>
      std::vector<T>  split(std::string &str, std::string const &key)
      {
        return split<T>(str, key.c_str());
      }
     
      template<typename T>
      std::vector<T>  split(std::string &str, const char *key)
      {
        std::vector<T>    result;
        size_t      pos;
        std::string     sub;
        int         size;
     
        if (!key)
          return result;
        size = strlen(key);
        while ((pos = str.find(key)) != std::string::npos)
          {
    	sub = str.substr(0, pos);
    	str.erase(0, pos + size);
    	if (!sub.empty())
    	  result.push_back(getValueFromString<T>(sub));
          }
        if (str != "")
          result.push_back(getValueFromString<T>(str));
        return result;
      }
     
      template<typename T>
      std::string   join(std::vector<T> const &c, std::string const &j)
      {
        return join<T>(c, j.c_str());
      }
     
      template<typename T>
      std::string   join(std::vector<T> const &c, const char *j)
      {
        std::ostringstream  os;
        unsigned int    i(0);
        std::string     str;
     
        if (!j)
          return str;
        while (i < c.size())
          {
    	os.str("");
    	os.clear();
    	os << c[i++];
    	str += os.str();
    	if (i < c.size())
    	  str += j;
          }
        return str;
      }
     
      template<typename T>
      T	getBetween(std::string const &c, const char *val1, const char *val2)
      {
        size_t		pos1, pos2;
     
        if (!val1 || !val2 ||
    	(pos1 = c.find(val1)) == std::string::npos ||
    	(pos2 = c.find(val2)) == std::string::npos)
          return getValueFromString<T>("");
        pos1 += strlen(val1);
        if (pos1 >= pos2)
          return getValueFromString<T>("");
        return getValueFromString<T>(c.substr(pos1, pos2 - pos1).c_str());
      }
     
      template<typename T>
      T	getBetween(const char *st, const char *val1, const char *val2)
      {
        std::string	str;
     
        if (st)
          str = st;
        return getBetween<T>(str, val1, val2);
      }
     
      template<typename T>
      std::vector<T>	getListOfItem(std::string st, const char *val1, const char *val2)
      {
        std::vector<T>	vec;
        std::string		str;
        size_t		find;
        int			length(val2 ? strlen(val2) : 0);
     
        str = getBetween<T>(st, val1, val2);
        for (;!str.empty(); str = getBetween<T>(st, val1, val2))
          {
    	vec.push_back(str);
    	if ((find = st.find(val1)) == std::string::npos)
    	  return vec;
    	st.erase(find, str.size() + length);
          }
        return vec;
      }
     
      template<typename T>
      std::vector<T>	getListOfItem(const char *st, const char *val1, const char *val2)
      {
        std::string	s;
     
        if (st)
          s = st;
        return getListOfItem<T>(s, val1, val2);
      }
     
      template<typename T>
      T &replace(T &s, std::string const &toReplace,
                 std::string const &replaced)
      {
        return replace<T>(s, toReplace.c_str(), replaced.c_str());
      }
     
      template<typename T>
      T &replace(T &s, const char *toReplace,
                 std::string const &replaced)
      {
        return replace<T>(s, toReplace, replaced.c_str());
      }
     
      template<typename T>
      T &replace(T &s, std::string const &toReplace,
                 const char *replaced)
      {
        return replace<T>(s, toReplace.c_str(), replaced);
      }
     
      template<typename T>
      T &replace(T &s, const char *toReplace, const char *replaced)
      {
        std::vector<T>  vec;
     
        if (!toReplace || !replaced)
          return s;
        vec = split<T>(s, toReplace);
        s = join<T>(vec, replaced);
        return s;
      }
    }
    C'est donc la fonction getValueFromString que je souhaiterais surcharger pour que lorsque le template serait std::string, il rentre dans ma surcharge pour ne pas passer par les stringstream (qui me poserait probleme car s'arrete au premier espace ou tabulation). Ce probleme s'appliquerait par exemple dans le getBetween.

    Merci d'avance !

  2. #2
    Membre éprouvé

    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    533
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 533
    Points : 1 086
    Points
    1 086
    Par défaut
    Surcharge... ou spécialisation ?

  3. #3
    Membre émérite
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    851
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2010
    Messages : 851
    Points : 2 293
    Points
    2 293
    Par défaut
    C'est exactement ce que je voulais, merci beaucoup !

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

Discussions similaires

  1. Fonction template et surchage
    Par LiquidHuk dans le forum C++
    Réponses: 8
    Dernier message: 04/07/2014, 16h42
  2. Pointeur sur une fonction template
    Par Progs dans le forum Langage
    Réponses: 2
    Dernier message: 15/02/2006, 21h25
  3. Fonction template virtuelle... comment l'éviter ?
    Par :Bronsky: dans le forum Langage
    Réponses: 12
    Dernier message: 07/06/2005, 15h21
  4. fonctions template
    Par romeo9423 dans le forum Langage
    Réponses: 12
    Dernier message: 22/01/2005, 17h13
  5. Fonctions template+friend sous VC7
    Par patapetz dans le forum MFC
    Réponses: 12
    Dernier message: 24/09/2004, 12h16

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