Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of class = void in C++? [duplicate]

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 { };
like image 493
msc Avatar asked May 17 '17 12:05

msc


People also ask

What is the purpose of the keyword void in C programming?

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.

Why void is useful?

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.

What is subclassing in c++?

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.

Is void a Class C++?

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.


1 Answers

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.

like image 61
Hatted Rooster Avatar answered Oct 17 '22 00:10

Hatted Rooster