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

117 Views Asked by At

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 the variable.

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

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

Remark: My question is different than this question. See the comments.

0

There are 0 best solutions below