Skip to content

Commit cb1c7b1

Browse files
committed
to_string(): implement overloads in the header .h file (not .cpp)
This is more foolproof, because it allows you to compile the `kaitaistream.cpp` library under `-std=c++98` and link to it from an application compiled under `-std=c++11` (and everything will still work fine). This was not possible before, see 4bc5896.
1 parent 850bd27 commit cb1c7b1

File tree

2 files changed

+24
-40
lines changed

2 files changed

+24
-40
lines changed

kaitai/kaitaistream.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -853,40 +853,6 @@ std::string kaitai::kstream::to_string_unsigned(uint64_t val) {
853853
return std::string(&buf[buf_contents_start], sizeof(buf) - buf_contents_start);
854854
}
855855

856-
// NB: the following 6 overloads are exactly the ones that
857-
// [`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string) has.
858-
// Testing has shown that they are all necessary: if you remove any of them, you will get
859-
// something like `error: call to 'to_string' is ambiguous` when trying to call `to_string`
860-
// with the integer type for which you removed the overload.
861-
862-
std::string kaitai::kstream::to_string(int val) {
863-
return to_string_signed(val);
864-
}
865-
866-
std::string kaitai::kstream::to_string(long val) {
867-
return to_string_signed(val);
868-
}
869-
870-
#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
871-
std::string kaitai::kstream::to_string(long long val) {
872-
return to_string_signed(val);
873-
}
874-
#endif
875-
876-
std::string kaitai::kstream::to_string(unsigned val) {
877-
return to_string_unsigned(val);
878-
}
879-
880-
std::string kaitai::kstream::to_string(unsigned long val) {
881-
return to_string_unsigned(val);
882-
}
883-
884-
#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
885-
std::string kaitai::kstream::to_string(unsigned long long val) {
886-
return to_string_unsigned(val);
887-
}
888-
#endif
889-
890856
int64_t kaitai::kstream::string_to_int(const std::string& str, int base) {
891857
char *str_end;
892858

kaitai/kaitaistream.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,29 @@ class kstream {
228228
*/
229229
static int mod(int a, int b);
230230

231+
// NB: the following 6 overloads of `to_string` are exactly the ones that
232+
// [`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string) has.
233+
// Testing has shown that they are all necessary: if you remove any of them, you will get
234+
// something like `error: call to 'to_string' is ambiguous` when trying to call `to_string`
235+
// with the integer type for which you removed the overload.
236+
231237
/**
232238
* Converts given integer `val` to a decimal string representation.
233239
* Should be used in place of `std::to_string(int)` (which is available only
234240
* since C++11) in older C++ implementations.
235241
*/
236-
static std::string to_string(int val);
242+
static std::string to_string(int val) {
243+
return to_string_signed(val);
244+
}
237245

238246
/**
239247
* Converts given integer `val` to a decimal string representation.
240248
* Should be used in place of `std::to_string(long)` (which is available only
241249
* since C++11) in older C++ implementations.
242250
*/
243-
static std::string to_string(long val);
251+
static std::string to_string(long val) {
252+
return to_string_signed(val);
253+
}
244254

245255
// The `long long` type is only available since C++11, so we use it only in C++11 mode.
246256
#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
@@ -249,22 +259,28 @@ class kstream {
249259
* Should be used in place of `std::to_string(long long)` (which is available only
250260
* since C++11) in older C++ implementations.
251261
*/
252-
static std::string to_string(long long val);
262+
static std::string to_string(long long val) {
263+
return to_string_signed(val);
264+
}
253265
#endif
254266

255267
/**
256268
* Converts given integer `val` to a decimal string representation.
257269
* Should be used in place of `std::to_string(unsigned)` (which is available only
258270
* since C++11) in older C++ implementations.
259271
*/
260-
static std::string to_string(unsigned val);
272+
static std::string to_string(unsigned val) {
273+
return to_string_unsigned(val);
274+
}
261275

262276
/**
263277
* Converts given integer `val` to a decimal string representation.
264278
* Should be used in place of `std::to_string(unsigned long)` (which is available only
265279
* since C++11) in older C++ implementations.
266280
*/
267-
static std::string to_string(unsigned long val);
281+
static std::string to_string(unsigned long val) {
282+
return to_string_unsigned(val);
283+
}
268284

269285
// The `unsigned long long` type is only available since C++11, so we use it only in C++11 mode.
270286
#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
@@ -273,7 +289,9 @@ class kstream {
273289
* Should be used in place of `std::to_string(unsigned long long)` (which is available only
274290
* since C++11) in older C++ implementations.
275291
*/
276-
static std::string to_string(unsigned long long val);
292+
static std::string to_string(unsigned long long val) {
293+
return to_string_unsigned(val);
294+
}
277295
#endif
278296

279297
/**

0 commit comments

Comments
 (0)