I found typename Enable = void
is defined in the ProtoBuf,
template<typename T, typename Enable = void>
struct RefTypeTraits;
However, I cannot find the Enable
is used in this header file, which confuse me. What does typename Enable = void
mean in template?
" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.
There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers. I myself prefer <typename T> as it more clearly describes its use; i.e. defining a template with a specific type.
The enable_if family of templates is a set of tools to allow a function template or a class template specialization to include or exclude itself from a set of matching functions or specializations based on properties of its template arguments.
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
It is to allow SFINAE with template specialization, as something like:
template <typename T>
struct RefTypeTraits<T, std::enable_if_t<some_condition<T>::value>>
{
// ... specialization for T which respects condition
};
Since C++20, we can specialize with concepts to avoid this needed extra template parameter.
Your template just has two template parameters. The second one is called "Enabled" and it has the default type of "void". This is a trick to allow SFINAE later on.
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