2

Validity of a non-template code is more or less clear to everyone - a compiler checks the syntax, types, and rules. The compiler has everything it needs to perform these checks.

But when the compiler has to compile a templated code (for example STL, or Boost) it does't have enough information. While the templated code isn't instantiated the compiler doesn't know what operations mean, whether or not they are defined for the types that will be used to instantiate this templated code, etc. Templated code can't be compiled untill instantiated.

Is there such a thing as a validity of a templated code? Would it require us to compile every instantiation of a templated code to verify its validity?

beginpluses
  • 497
  • 2
  • 9
  • 2
    Silly question: What exactly do you mean by "validity"? – paulsm4 May 30 '19 at 20:26
  • 2
    All the compiler does is check if a template is syntactically correct. Once it needs to instantiate a template, then the code is checked again against the concrete type(s). – NathanOliver May 30 '19 at 20:29
  • 1
    "Would it require us to compile every instantiation of a templated code to verify its validity?" That's not possible - `template struct Foo;` would have to test `Foo` for all types, even `Foo>`, `Foo>>` and so on... Actually a lot of C++ templates rely on not being valid for some arguments. On how it's done , see [two-phase lookup](https://stackoverflow.com/questions/7767626/two-phase-lookup-explanation-needed). – Quimby May 30 '19 at 20:33
  • 1
    @NathanOliver, with two-phase lookup a compiler can have some more work. – Evg May 30 '19 at 20:36
  • @Evg Isn't that exactly what I described? I'm not sure how that is any different then what I said. – NathanOliver May 30 '19 at 20:37
  • @NathanOliver, syntactic check is not *all* the compiler does. – Evg May 30 '19 at 20:38
  • @Evg Phase one that is all that it can do. – NathanOliver May 30 '19 at 20:40
  • 1
    In general, you only need to successfully instantiate it once to prove that the template is valid. Syntactical correctness alone does not suffice. It's a well-known pitfall to write `static_assert(false, "")` and find that the compiler rejects this statically well-formed construct in a template. – Johannes Schaub - litb May 30 '19 at 20:41
  • @NathanOliver, and during that phase it can reject some templated code. – Evg May 30 '19 at 20:41
  • @NathanOliver, this is what I mean: https://stackoverflow.com/a/56025280/1625187 – Evg May 30 '19 at 20:43

1 Answers1

0

The standard talks about such validity:

The validity of a template may be checked prior to any instantiation. [ Note: Knowing which names are type names allows the syntax of every template to be checked in this way. — end note ]

As the note says, the reliable check is merely syntactic (or grammar-based). Even rules like looking up non-dependent names are covered by the rule that any template that cannot be instantiated is ill-formed, no diagnostic required. The implementation may then do anything at all, including compiling a program that does… something. (This is really too much implementation freedom; some of these rules could just be “may be ill-formed (with a diagnostic) at the implementation’s discretion”.)

Davis Herring
  • 36,443
  • 4
  • 48
  • 76