C++ Logo


Advanced search

When is semantic correctness checked in a template?

From: Myria <myriachan_at_[hidden]>
Date: Tue, 6 Apr 2021 17:57:43 -0700
When does a template's semantic correctness get checked (for
non-dependent expressions)? If an identifier naming a type is used in
a template in a way that is valid at template definition time, but the
type of the identifier changes prior to instantiation, what happens?

Note that this is not the case of name lookup changing, but rather the
actual type of the identifier.

In the following example, "a" starts out as "array of int of unknown
bound" but becomes "array of int of bound 2" later in the program.
Does "r" becoming bound to "a" become ill-formed for template
instantiations that occur after "a" has its type change?

extern int a[];

template <int I>
int t()
    int (&r)[] = a; // important line
    return r[0] + I;

int f()
    return t<4>();

int a[2];

int g()
    return t<5>(); // is t's "r" binding well-formed?


Received on 2021-04-06 19:57:56