Reliably check for whether a class template has been instantiated, with C++11?
Asked Answered
M

0

7

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:

template <typename T> struct MyStruct { };

// some magic 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. Also, the user who wrote the accepted answer for that question has left SO and will not be updating it...

So, my question: Is it possible to reliably check whether a class/struct template has been instantiated for a certain type?

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.
Middlesworth answered 3/4, 2020 at 14:29 Comment(1)
@walnut: Ok, so, I'll just drop the function templates extra-question, for better focus here and not to confuse people.Middlesworth

© 2022 - 2024 — McMap. All rights reserved.