I would like to disallow the class templates for all the types except types class is specialized for with using static assert. Please the check the code below:
template <typename Type>
class foo {
static_assert(false, "Wrong Type");
};
template <>
class foo<int> {
};
int main()
{
foo<int> fi; // should compile
foo<double> fd; // should not compile
return 0;
}
But it is not working. After some research I found a question answering my question: Enforce template type through static_assert
I have update the code according the question:
template <typename Type>
class foo {
static_assert(sizeof(Type) == -1, "Wrong Type!");
};
template <>
class foo<int> {
};
int main()
{
foo<int> fi; // should compile
foo<double> fd; // should not compile
return 0;
}
My question is why the second version is OK and why not the second one?