void

Hi
In almost all the books about C and C++, void is introduced as a data type, though comparitively it doesn't seem to be a data type like int and double; Could you clarify this for me?
Thanks in advance
It is just my opinion that it is wrong to call it a type because it doesn't fulfill all the basic requirements of a data type. Again, just my opinion.

It is fallacious to say that void is a type because you can declare a pointer of void just by making note of this ability present in other data types. Just because A implies B, doesn't mean that B implies A. One of the most common fallacies.

If there are OTHER reasons added to back up void as a type, well, then I'd love to hear them. Otherwise I keep my opinion as is.
void is what you return or maybe I should say not return.

if you have a function my_first_function

void my_first_function(void);

what we do here is the function will not take any variables and will not return a variable.

you can't create a variable void and give it a value
From what I've read and learned is that void is a data type, or the lack there of. It takes up no memory, that I'm aware of, has no physical value, again, that I'm aware of, and can't be passed, modified, or used in anyway aside from pointers.

I believe this would be open to a debate, but void exists to replace data types when there isn't supposed to be a data type. That is why it's used in functions. We don't always need a data type returned.
On the other hand a void pointer is a placeholder for any type of data. Sure, void itself is quite useless, but in advanced programming techniques very essential.
void is a type. It is not an arithmetic type like int and double. It is not a scalar type, like pointer or enum, it is not a compound type like reference or function. But it is a distinct fundamental type.
I think it is treated as an incomplete type.

void can be used for things other than return types of functions and pointers to cv void:
1
2
3
4
5
6
7
8
9
10
11
12
13
#define nonfatal_assert(e) (e) ? void(0) : \
    void( std::cerr << "assertion '" << #e << "' failed! continuing...\n" ) ;

template < typename T = void > void foo( int a, T* b = nullptr )
{
    nonfatal_assert( a < 100 ) ;

    typeid( void(a) ) == typeid( void(std::strlen) ) ? void(0) : void( throw "error" ) ;

    typedef decltype( void("Microsoft") ) VOID ;

    return void( ( void(4.6), a ) + 5 ) ;
}
I think it is a matter of classification of topics to include void amongst other data types, however, maybe it seems unwontedly to know it a data type...
Anyway, thanks a lot buddies
Topic archived. No new replies allowed.