This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Is g++ not conforming to the C++ standard here?
- From: Philipp Thomas <pthomas at suse dot de>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 25 Aug 2003 16:44:56 +0200
- Subject: Is g++ not conforming to the C++ standard here?
- Organization: SuSE Linux AG
I've got an interesting question from one of our customers.
ISO/IEC 14882:1998 in 14.6#9 says:
If a name does not depend on a template-parameter (as defined in 14.6.2),
a declaration (or set of declarations) for that name shall be in scope at
the point where the name appears in the template definition; the name is
bound to the declaration (or declarations) found at that point and this
binding is not affected by declarations that are visible at the point of
instantiation.
[Example:
void f(char);
template<class T> void g(T t)
{
f(1); // f(char)
f(T(1)); //dependent
f(t); //dependent
dd++; //not dependent
// error: declaration for dd not found
}
void f(int);
double dd;
void h()
{
g(2); //will cause one call of f(char) followed
// by two calls of f(int)
g( a ); //will cause three calls of f(char)
}
end example]
I've modified this example a bit to make it clear what gets called (and remove a compiler
error):
-------------------------------------------------------------
#include <cstdio>
#include <cctype>
void f(char c)
{
if(isprint(c))
printf("f(char) called : %c\n", c);
else
printf("f(char) called : 0x%x\n", c);
}
template<class T> void g(T t)
{
f(1); // f(char)
f(T(1)); //dependent
f(t); //dependent
}
void f(int i)
{
printf("f(int) called : %d\n", i);
}
int main()
{
g(2); // will cause one call of f(char) followed
// by two calls of f(int)
puts("");
g('a'); // will cause three calls of f(char)
return 0;
}
--------------------------------------------------------------
And it outputs
f(int) called : 1
f(int) called : 1
f(int) called : 2
f(int) called : 1
f(char) called : 0x1
f(char) called : a
So it seems like gcc 3.3.1 doesn't get this right, does it?
BTW, seems that the same 3.3.1 bug lets the following output "22" instead of
the expected "12".
#include <cstdio>
void f(double)
{
printf("1");
}
template <class T> void foo(T t)
{
f(1); // this should result in a call to f(double)
f(t); // and this to f(int)
}
void f(int)
{
printf("2");
}
int main ()
{
foo(1);
}
Philipp
--
Philipp Thomas <pthomas@suse.de>
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nuremberg, Germany