Précédent   Forum du club des développeurs et IT Pro > C et C++ > C++ > Langage
Langage Langage C++, Programmation Orientée Objet, Templates, etc. Avant de poster : FAQ C++
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 27/02/2013, 13h02   #1
imperio
Membre éclairé
 
Avatar de imperio
 
Homme Guillaume Gomez
Étudiant
Inscription : mai 2010
Messages : 161
Détails du profil
Informations personnelles :
Nom : Homme Guillaume Gomez
Localisation : France

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

Informations forums :
Inscription : mai 2010
Messages : 161
Points : 329
Points : 329
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 :
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 !
imperio est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/02/2013, 13h28   #2
cob59
Membre chevronné
 
Inscription : décembre 2008
Messages : 490
Détails du profil
Informations forums :
Inscription : décembre 2008
Messages : 490
Points : 751
Points : 751
Surcharge... ou spécialisation ?
cob59 est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 27/02/2013, 13h38   #3
imperio
Membre éclairé
 
Avatar de imperio
 
Homme Guillaume Gomez
Étudiant
Inscription : mai 2010
Messages : 161
Détails du profil
Informations personnelles :
Nom : Homme Guillaume Gomez
Localisation : France

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

Informations forums :
Inscription : mai 2010
Messages : 161
Points : 329
Points : 329
C'est exactement ce que je voulais, merci beaucoup !
imperio est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 05h43.


 
 
 
 
Partenaires

Hébergement Web