Do you see why the static_assert fail:
template <typename T>
void
foo(const T &c)
{
static_assert(std::is_base_of<T, char>::value, "T must be char"); // Fails !
}
int main()
{
char c = 'a';
foo<char>(c);
return 0;
}
I swapped T and "char", still fails.
You might want to consider adding additional checks:
template <typename T> void foo(const T &c)
{
static_assert(
std::is_base_of<T, char>::value || std::is_same<T, char>::value,
"T must be char");
}
But if you are only concerned about chars, then you might as well do:
static_assert(std::is_same<T, char>::value, "T must be char");
Also consider is_fundamental
, and is_convertible
for more complex assertions.
The documentation for std::is_base_of
states:
If Derived is derived from Base or if both are the same non-union class, provides the member constant value equal to true. Otherwise value is false.
But char
is not a class : it is a primitive type, so this is the expected result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With