question

JerryM-2071 avatar image
0 Votes"
JerryM-2071 asked IgorTandetnik-1300 commented

Improvement of the C++ language proposal :) :) :)

Hi boys and girls,
I would like to propose new key word of the C++ language: "dynamictype". It is usefull in type-cast operation(s) such us:

int myvariable = 0;
dynamictype mytype;
mytype = typeof(float);
std::cout << dynamic_cast<mytype>(myvariable);

Here is a complex example:


//*

 #include <iostream>
 #include <string>
 #include <iterator>
 #include <algorithm>
 #include <vector>
    
 struct Example_1 {
    
     int item1;
     float item2;
 };
    
 struct Example_2 {
     unsigned long item1;
     int item2;
 };
    
 struct arrItem {
     int key;
     dynamictype Decl;
     void* link;
 };
    
    
 int main()
 {
    
     arrItem *item = nullptr;
    
     Example_1 *e1 = nullptr;
     Example_2 *e2 = nullptr;
    
     std::vector<arrItem> DataList; 
    
     // datovy typ 0 je Example_1
     e1 = new Example_1();
     e1->item1 = 101;
     e1->item2 = (float)102.10;
    
     DataList.push_back(arrItem());
     DataList[0].key = 0;
     DataList[0].typeDecl = typeof( Example_1 );
     DataList[0].link = e1;
    
     // datovy typ 1 je Example_2
     e2 = new Example_2();
     e2->item1 = 201;
     e2->item2 = (int)202;
    
     DataList.push_back(arrItem());
     DataList[1].key = 1;
     DataList[1].typeDecl = typeof( Example_2 );
     DataList[0].link = e2;
    
     for (int i = 0; i < (int)DataList.size(); i++) {
    
         std::cout << "Example_1: key: " 
                   << DataList[i].key 
                   << "; item1: " 
                   << declarative_cast<DataList[i].typeDecl>(DataList[i].link)->item1; 
                   << "; item2: " 
                   << declarative_cast<DataList[i].typeDecl>(DataList[i].link)->item2; 
         std::cout << "\n";
    
     }// for 
    
    
     delete(DataList[0].link);
     DataList[0].link = nullptr;
     delete(DataList[1].link);
     DataList[1].link = nullptr;
    
     DataList.clear();
    
    
 }// int main()
c++
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

C++ fundamentally doesn't work this way. Every expression must have a type known at compile time, but you envision expressions like declarative_cast<DataList[i].typeDecl>(DataList[i].link)->item1 whose type is only known at run time.

Moreover, you are passing this expression as an argument to a set of overloaded functions and function templates (`operator<<` in this case). How is the compiler supposed to choose the right overload? Is it supposed to include into the compiled binary an infinite number of template instantiations for every possible type, just in case the argument turns out to be that type at run time?


Distantly related, delete(DataList[0].link) exhibits undefined behavior. delete must be used on a pointer of the same type that was allocated with new, or its base type if that base type has a virtual destructor.

1 Vote 1 ·

How will it look without the new construct?


0 Votes 0 ·

@JerryM-2071

As far as I'm concerned you could post the request to DC for better help.


0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered Viorel-1 commented

Standard C++ is the organization for the C++ language standard. See How To Submit a Proposal : Standard C++; it is not easy to do and it should not be. Also see the PDF at How to Write a C++ Language Extension Proposal, written by Bjarne Stroustrup, the original designer and implementer of the C++ language.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


But it is probably worth discussing and evaluating here too.


0 Votes 0 ·

The Q&A (these) forums are designed for questions and answers but not discussion.

0 Votes 0 ·

Thank you for indisputable answers.

0 Votes 0 ·
JerryM-2071 avatar image
0 Votes"
JerryM-2071 answered

wooooooou your answers are really "indisputable" :)
Jerry

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.