tags:

views:

50

answers:

1

This is the statement from ISO C++ Standard 14.6/7:

Knowing which names are type names allows the syntax of every template
definition to be checked. No diagnostic shall be issued for a template definition for which a valid specialization can be generated. If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required. If a type used in a non-dependent name is incomplete at the point at which a template is defined but is complete at the point
at which an instantiation is done, and if the completeness of that type
affects whether or not the program is well-formed or affects the semantics
of the program, the program is ill-formed; no diagnostic is required. [Note: if a template is instantiated,errors will be diagnosed according to the other rules in this Standard. Exactly when these errors are diagnosed is a quality of implementation issue. ]

Example:

    int j;
    template<class T> class X {
               // ...
               void f(T t, int i, char* p)
               {
                                         // diagnosed if X::f is instantiated
                           t = i;
                                         // and the assignment to t is an error
                                         // may be diagnosed even if X::f is
                           p = i;
                                         // not instantiated
                                         // may be diagnosed even if X::f is
                           p = j;
                                         // not instantiated
               }
               void g(T t) {
                                         // may be diagnosed even if X::g is
                           +;
                                         // not instantiated
               }
    };

(mostly failed cases) can any one tell some more examples for this statement ..like this..please?

+2  A: 
void f<T>() "I am an ill-formed 'template definition' parameterized on T.";

An implementation is allowed to accept the above as a syntactically ill-formed template definition and not diagnose it until it's actually instantiated. Hope this explains it. (Of course I'm kidding, but I'm not entirely unserious. It shows a flaw of the above quoted text: There is no "template definition" that can contain ";+;").

The other thing about incomplete types says the following is ill-formed but no diagnostic is required

struct foo;

template<typename T>
void f() { foo x; }
  // foo is incomplete here

struct foo { };
  // foo is complete here

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

The "no diagnostic required" rules in the Standard grants an implementation to behave in any way it sees fit (this makes any program that violates a rule for which no diagnostic is required to have effectively undefined behavior). As a result, the text you quoted really is (IMHO) badly lawyered.

See Confused about ill-formed templates and Compiling C++ templates as opposed to preprocessing them (with Digital Mars fame Walter Bright)

Johannes Schaub - litb