vendredi 3 avril 2020

Reliably check for whether a class template has been instantiated in C++?

It used to be possible, using some C++ compilers, to check whether a templated type has already been instantiated, so that the following program syntax compiles without error:

// magic (without macros) goes here

int main () {
    static_assert(!is_instantiated<MyStruct<int>>(), "failure");
    MyStruct<int> a;
    static_assert(is_instantiated<MyStruct<int>>(), "failure");
}

The "magic" was in the solution to this question:

Compile time template instantiation check

However - that no longer works (Godbolt.org) with recent GCC and Clang versions.

My question:

  • Is it possible to reliably check whether a class/struct template has been instantiated for a certain type?
  • If so, is that solution also usable for function templates? (If not I might ask a separate question)

Notes:

  • Looking for a solution using C++11 - not anything later.
  • "It's impossible" is a valid answer, but - only if you can justify it.
  • Related: This question.
  • If it makes it easier, you may assume that the class is possible to instantiate; but it's better if you don't.

Aucun commentaire:

Enregistrer un commentaire