I can specialise a destructor for one case, but I'm having trouble telling the compiler to just use a normal destructor for any other cases:
#include <iostream>
template <int = 0>
struct Foo
{
~Foo();
};
int main()
{
{
Foo<> a; // Normal destructor called
}
{
Foo<7> a; // Special destructor called
}
}
template<>
Foo<7>::~Foo() { std::cout << "Special Foo"; }
template<>
Foo<>::~Foo() {} // Normal destructor does nothing.
This works fine, but now if I add another template argument, for example Foo<3> a; then the linker says that it can't find the destructor definition. How can I just say I want a special destructor just for number 7, and handle any other cases with a normal destructor?
I tried:
Foo::~Foo() {} // Argument list missing
Foo<int>::~Foo() {} // Illegal type for non-type template parameter
template<>
Foo<int>::~Foo() {} // Same thing
template<int>
Foo<>::~Foo() {} // Function template has already been defined