C ++ Void non-pointer

I was wondering why can't there be a void data type that is not a pointer?

Of course, you could pass a target of a certain size, having

void4
void8
void32

And then it is only allowed to "pour" the void data type into another class if its size is equal to or less than the size of the classes.

Is there something that I am missing, or does the C ++ committee simply consider this a bad practice?

EDIT:

I didn’t explain myself very well, so I’ll give an example of its use:

 main()
{
    /* 

     Lets make a list of unknown elements

     std::string is 8 bytes, and float is 4
     bytes, so we'll reserve 8 byte sequences

     */

    vector<void8> elements;

    elements.push_back((void8) string("First string element"));

    elements.push_back((void8) float(5.76) );

    elements.push_back((void8) string("Third string element"));

    // Ect.

    cout << (string) elements[0];
    cout << (float) elements[1];
    cout << (string) elements[2];
    cout << (float) elements[2]; // Garbage


    void1 data;

    data = (void1) bool(1);
    data = (void1) unsigned int(80094); // Error, not enough size
}

Its named void because you don't know what type it currently stores, similar to the void pointer.

+3
source share
10 answers

, "" . C ++ " " ( ), ", " ( ).

", , ", , . , ( ), , :

struct void8 { char bytes[8]; };

reinterpret_cast, POD , , . -POD-, , undefined, void*, -POD-.

, , , - boost::any, . "void" ++.

+3

boost:: variant boost:: any. + sizeof () .

+8

"void"? , , , , .

+2

? , . Void32, lol! void , . "procedure" "sub". void - ( void). .

: , . void32 * - - 32 , void8 * - 8- .. int32 *, char * .

+2

, , . char. char , char - ++ . , , , , void .

void , , . , . void4 . "", . , , . , , POD, , . , , . , , , void.

, , :

T?

void foo(void* p) {
   T q = *p;
}

void4? void32? void? ? ? POD?

+2

void . , , void.

, , , void.

0

void* , , . , .

void . . .

, , . . .

, , int, . - .

0

void "" , void. , void . void .

: C ++ void "". - , - void , - .

, void -is-nothing void: , GNU, void*, char*, void*, , void , .

, , : . void , .

0

, , , , , .

, . , , , "" , .

, , , , .

0

, , , .

(void)func1();

void x;
x = func1();

void func2() {
   void x;
   ...
   return x;
}

int func3(int x, void y, char z);

void x = func3(1,2);
x = func3(1,x,2);

void

, .

0
source

Source: https://habr.com/ru/post/1761492/


All Articles