I am trying to understand the usefulness of static_assert , and I want to know if this can help me in ensuring the execution of the design, and if so, how.
I have a generic template class that hides its own implementation inside another template class, which partially specializes based on the size of the template type. Here is a brief description of this project:
template <class T, size_t S = sizeof(T)> struct Helper; template <class T> struct Helper<T, sizeof(long)> { static T bar(); };
Foo is only supported if size T supported by the Helper specialization. For example, Foo<long> and Foo<unsigned long> supported. However, suppose the user is trying to build a Foo<bool> . This usually causes errors because the Helper specialization for bool not defined, which is the intended behavior.
Can static_assert be used in this project to provide the user with more useful errors?
In addition, I would also like to limit the user to a specific type, although the size may be correct. For example, Foo<float> not allowed. Right now, the only way I know to provide this is with a bold comment in the documentation. :)
c ++ templates static-assert template-specialization
Zeenobit
source share