What is the purpose of the class = void
in the following code snippets?
template< class, class = void >
struct has_type_member : false_type { };
template< class T >
struct has_type_member<T, void_t<typename T::type>> : true_type { };
When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal." If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword.
Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.
Sub Class: The class that inherits properties from another class is called Subclass or Derived Class. Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
what is difference between void and class??? void is a data type which means "No value" . we used void as a keyword to indicate that the function will not contain any return value. while class is a user defined type which contains data and functions in it.
template< class, class = void >
struct has_type_member : false_type { };
That's your default struct template, it asks for 2 template arguments but the second one is set to void
as default, so this argument does not need to be specified explicitly, somewhat like a default function parameter.
Then :
template< class T >
struct has_type_member<T, void_t<typename T::type>> : true_type { };
Is a template specialization for your has_type_member
struct, SFINAE will rule out this specialization if T::type
doesn't exist (and thus, is invalid syntax), if it does exist it will pick this specialization otherwise.
The 2nd parameter is necessary to be used for template specialization, but we don't use it in our "fallback" struct
so we just default to void
.
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