2

just like an array means this data structure support return the element of certain index in O(1) times.different types mean both builtin type and user defined class which should use reference.

for example, A is the class which have made this structure down. The b,c is instance of different classes. And the A should support follow operate:

A a(10);//apply for storage of 10 elements

a[0]=120;

a[1]=’a’;

a[2]=”adsad”;

a[3]=b;

a[3]=c;

a[1]=c;

a[2]=123.5;

…

this question is not for the practical use, just for the curiosity.

now I have known how can solve this. it is really cool:D

Cœur
  • 37,241
  • 25
  • 195
  • 267
yodahaji
  • 717
  • 1
  • 5
  • 10
  • 10
    The phrase you are looking for is a [heterogeneous container](https://stackoverflow.com/questions/7804955/heterogeneous-containers-in-c) and can be achieved using things like [`std::any`](http://en.cppreference.com/w/cpp/utility/any) but I'd suggest you strongly consider *why* you need this in your design – Cory Kramer Sep 21 '17 at 15:16
  • 7
    What you are looking for is a `std::variant`, or perhaps `std::any`, which is new to C++17. – Sam Varshavchik Sep 21 '17 at 15:17
  • structure having map indexed by type, sometimes called `context`, Im not sure I understand U – Jacek Cz Sep 21 '17 at 15:17
  • I can't see any use case where you put somewhere data into something, get it back again and don't need to know what kind of data type it has. And also with std::any you have to know how to get the data back again. So what is the magic needed for in the mid of this path? Maybe I am wrong, but it sounds like very bad design... – Klaus Sep 21 '17 at 16:14
  • @ CoryKramer , it is not for the reason of practical use,just for the curiosity:D – yodahaji Sep 21 '17 at 16:20

1 Answers1

4

You could do that with an std::vector or an std::array of std::any.

Like the following:

std::vector<std::any> many_any(10);
many_any[0] = 120;
many_any[1] = 'a';
many_any[2] = std::string("adsad");
many_any[3] = 123.5;

std::cout << std::any_cast<int>(many_any[0]) << std::endl;
std::cout << std::any_cast<char>(many_any[1]) << std::endl;
std::cout << std::any_cast<std::string>(many_any[2]) << std::endl;
std::cout << std::any_cast<double>(many_any[3]) << std::endl;

This code outputs:

 120
 a
 adsad
 123.5

And here is a live-example.


std::any is a heterogeneous container that uses type-erasure to store any type. It is a part of C++17 standard. Here is a series of articles that explains how type-erasure could be implemented in C++: Type erasure — Part I.

Important Note: You should consider very carefully if you really need std::any. As it is more of a "yes, we can!" feature, which is rarely useful in the pure form. Other options include storing objects in a polymorphic setting: either using interfaces (which can also be implemented using type-erasure) or variant+visitor.

Update: if your compiler does not support C++17 you can take a look at Boost's any implementation.

AMA
  • 4,114
  • 18
  • 32
  • wow! it seems perfectly solved my question. it is so cool!! but my compiler seems not support the feature in the c++17. could you tell me this support user defined classes? if yes,my problem has been perfectly resolved. – yodahaji Sep 21 '17 at 16:09
  • @yodahaji I've updated the answer with a mention of `boost::any`. – AMA Sep 21 '17 at 16:16
  • @yodahaji If it answers your question you can accept the answer by clicking a check-mark under the votes. This will let other people know that you found what you were looking for and will give you and the person who answered some rep. You don't have to do this. I just would like to mention this, since you don't have any accepted answers yet and might not be aware. – AMA Sep 21 '17 at 16:16
  • thanks for reminding,i will note that in the future. – yodahaji Sep 21 '17 at 16:18
  • @yodahaji yes, it should support user-defined types too – AMA Sep 21 '17 at 16:29