Re: function templates are not being compiled !



Vladimir Nesterovsky wrote:
template <class T>
void f()
{
    g();
}

int main()
{
}

That code is ill-formed, since there is no g() visible, but it is up to
the compiler whether it diagnoses the error or not.


Tom,

"g()" might be a call to a "void g(T t = T());" and therefore it's template
dependant.

No, it might not, since default arguments are never used as part of template argument deduction, and in any case, the use of "g" does not match any uses of a name that make it a dependent-name in the C++ standard. It might indeed be an attempted call to void g(int i = 4), double g(std::string foo = "Hello") or int g(), but since there is no such function declared before the definition of f(), any such definition won't be found by name lookup, and therefore the code is ill-formed.


Another similar example:

template <class T>
void f()
{
    g();
}

void g()
{
}

int main()
{
    f<int>();
}

Unfortunately, VC7.1 compiles that successfully, even though it is ill-formed, and does require a diagnostic. This is because VC7.1 doesn't have non-dependent name lookup implemented. That code fails to compile on other popular, more standards compliant compilers, like GCC 3.4+ and Intel C++.

Tom
.



Relevant Pages

  • Re: fields for methods?
    ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
    (comp.programming)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Re: C Questions
    ... No prototype is given for this function, so the compiler cannot ... To prototype a function with no parameters, use void: ... %d takes an int argument, ...
    (comp.lang.c)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... This is non-standard (Conceivably your compiler allows it ... tmp1 might not be correctly aligned for u32. ... void change_endian{ ... typedef unsigned int u32; ...
    (comp.lang.c)
  • Re: The_Sage & void main()
    ... shall have a return type of type int but otherwise in all other respects ... > main may have the return type void then main may indeed have the ... If the compiler accepts void main, then you may use void main ... about whether void mainis conforming or not. ...
    (comp.lang.cpp)