Topic: Call to static function from template function
Author: Michiel.Salters@cmg.nl (Michiel Salters)
Date: Mon, 25 Mar 2002 19:27:54 GMT Raw View
Charles Sanders <C.Sanders@BoM.GOV.AU> wrote in message news:<3C9AEC6B.C2929990@BoM.GOV.AU>...
> The following code compiles with some compilers and not with
> others, and is a much cut down version of code from a third
> party that I have to compile. The question is, is the code
> correct according to the standard or not.
> The error message from the IBM compiler suggests that the
> standard requires that static (file scope) functions not be included
> in the search during template functions instantiations in some cases.
<quote>
[temp.dep.candidate] 14.6.4.2 Candidate functions
1 For a function call that depends on a template parameter, if the function
name is an unqualified-id but not a template-id, the candidate functions
are found using the usual lookup rules (3.4.1, 3.4.2) except that:
For the part of the lookup using unqualified name lookup (3.4.1), only
function declarations with external linkage from the template definition
context are found.
For the part of the lookup using associated namespaces (3.4.2), only
function declarations with external linkage found in either the template
definition context or the template instantiation context are found.
</quote>
"Static functions" have internal, not external linkage, and the function
call depends on template parameter A, so it is an error.
HTH,
--
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Charles Sanders <C.Sanders@BoM.GOV.AU>
Date: Fri, 22 Mar 2002 16:54:40 GMT Raw View
I do not know if this is the best news group to pose this question
on, or if I should post it to comp.lang.c++.
The following code compiles with some compilers and not with
others, and is a much cut down version of code from a third
party that I have to compile. The question is, is the code
correct according to the standard or not. I have looked at
various FAQ's and news groups, and consulted other C++
programmers without success. If it is in fact a compiler error,
I want to report it to the vendor, but I do not want to do so
if the compiler is in fact correct.
The error message from the IBM compiler suggests that the
standard requires that static (file scope) functions not be included
in the search during template functions instantiations in some cases.
Is this correct ?
If so, when are static functions not used ?
static int f1( int i )
{
return i;
}
template< class A > int f2( int(*p)(A), A a )
{
return f1( p(a) );
}
static int f3( double d )
{
return (int)d;
}
int main( int ac, char **av )
{
double x = 2.0;
return f2( f3, x );
}
Results with various compilers:
aCC: HP ANSI C++ B3910B A.03.33 on HP-UX 11.11
Error 328: "a1.cc", line 10 # Function 'f1' has not been defined
yet; cannot call.
return f1( p(a) );
^^
No error
if "static int f1( int i )" is changed to "int f1( int i )"
or "return f1( p(a) );" is changed to "int j=p(a); return f1(j);"
or "return f1( p(a) );" is changed to "return f1( (int)p(a) );"
aCC: HP ANSI C++ B3910B A.01.21 on HP-UX 10.20
No error
g++ 3.0.1 on HP-UX 11.11
No error
g++ 3.0.4 on HP-UX 11.11
No error
g++ 2.95.2 on HP-UX 10.20
No error
g++ 2.7.2 on AIX 3.4
No error
xlC version 5 on AIX 3.4
"a1.cc", line 10.8: 1540-0274 (S) The name lookup for "f1" did not
find a declaration.
"a1.cc", line 10.8: 1540-1292 (I) Static declarations are not
considered for a function call if the function is not qualified.
No error
if "static int f1( int i )" is changed to "int f1( int i )"
or "return f1( p(a) );" is changed to "int j=p(a); return f1(j);"
--
Charles Sanders
National Meteorological and Oceanographic Centre
Bureau of Meteorology Email: C.Sanders@BoM.GOV.AU
GPO Box 1289K Phone: + 61 3 9669 4040
Melbourne 3001 Australia Fax: + 61 3 9669 4023
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]