Hello,
I'm using "Sun C++ 5.10 SunOS_i386 2009/06/03" under OpenSolaris snv_111b and
have a question about passing standard library functions to a template.
Consider this small example:
% cat -n anachron_warn.cpp
1 #include <string>
2 #include <assert.h>
3 #include <stdlib.h>
4
5 template <typename T>
6 bool ToNumber(const std::string& s, T *val, T (*convFunc)(const char *, char **, int))
7 {
8 const char *start = s.c_str();
9 char *end;
10 *val = convFunc(start, &end, 10);
11
12 // return true only is scan was stopped by the terminating NUL and if the
13 // string was not empty to begin with
14 return !*end && end != start;
15 }
16
17 int main()
18 {
19 long l;
20 assert( ToNumber("12345", &l, strtol) );
21 assert( l == 12345 );
22
23 long long ll;
24 assert( ToNumber("12345678901234", &ll, strtoll) );
25 assert( ll == 12345678901234);
26
27 assert( !ToNumber("17abc", &l, strtol) );
28
29 return 0;
30 }
31
% CC anachron_warn.cpp
"anachron_warn.cpp", line 20: Warning (Anachronism): Formal argument convFunc of type long(*)(const char*,char**,int) in call to ToNumber<long>(const std::string &, long*, long(*)(const char*,char**,int)) is being passed extern "C" long(*)(const char*,char**,int).
"anachron_warn.cpp", line 24: Warning (Anachronism): Formal argument convFunc of type long long(*)(const char*,char**,int) in call to ToNumber<long long>(const std::string &, long long*, long long(*)(const char*,char**,int)) is being passed extern "C" long long(*)(const char*,char**,int).
"anachron_warn.cpp", line 27: Warning (Anachronism): Formal argument convFunc of type long(*)(const char*,char**,int) in call to ToNumber<long>(const std::string &, long*, long(*)(const char*,char**,int)) is being passed extern "C" long(*)(const char*,char**,int).
3 Warning(s) detected.
The warning is admittedly valid (although I never understood its usefulness
to be honest). However how can I avoid it? First of all, I thought that using
std::strtol &c would be enough. But to my surprise it wasn't, the warning is
exactly the same if I replace <stdlib.h> with <cstdlib> and use the functions
in std namespace instead of the global one. Looking at /usr/include/iso/
stdlin_iso.h explained it: the functions in std namespace are still declared
inside extern "C" there. I'm not sure if this is correct but it does mean that
using them instead of the functions in the global namespace won't help.
But what else can I do? Any idea about any workarounds or a possibility to
disable this particular warning (I do like many other warnings produced by
this compiler)?
Thanks in advance,
VZ