dimanche 5 avril 2020

Which part of the code to consider when instantiating a variable template?

In the following program the global variable isCompleteType<Apple> is initialized differently by clang and gcc (live on godbolt.org):

template <class T>
constexpr bool IsCompleteTypeHelper (decltype (sizeof (T))) { return true; }

template <class T>
constexpr bool IsCompleteTypeHelper (...)                   { return false; }

template <class T>
bool isCompleteType = IsCompleteTypeHelper<T> (0);


class Apple;


int main ()
{
    return isCompleteType<Apple>;
}


class Apple {};
  • Clang 10.0.0 initializes isCompleteType<Apple> to true.
  • Gcc 9.3 initializes isCompleteType<Apple> to false.

Since the definition of Apple – which could make the variable true – is after the instantiation of isCompleteType, I concluded that the compilers do the following when they initialize isCompleteType<Apple>.

  • Clang considers the whole translation unit.
  • Gcc considers only the code above the instantiation.

Which compiler is right? Why? Could you quote the standard?

Aucun commentaire:

Enregistrer un commentaire