container to hold numerous data types

Pages: 12
In python, lists allow any object or rather data type to be inserted into it, while also allowing to shrink and grow dynamically. Is there an equivalent in c++?

code wise
1
2
3
4
5
6
vector<ANY> v;
v.push_back("TEST")
v.push_back('\n')
v.push_back(10.1)
Klass obj;
v.push_back(obj)


Would there be somehting like this, where ANY could be of any data type?
Last edited on
metulburr wrote:
Is there an equivalent in c++?

Nothing standard. That's not what language C++ is. C++ is a statically typed, vs Python's dynamically typed nature. It's possible to make it happen by just making an Object super class, and deriving any class you want in a common container.

I personally don't see the point of a container that you just throw any old data into.
I personally don't see the point of a container that you just throw any old data into.


its just easier that way. I love dynamic codes. learning C++ has been a challenge.
But why would you need a container to hold vastly different data types? To me that just beats the point of having a container in the first place.

Anyways, better to use the language how it was designed, and not try to hack it to make it fit your need. If you need dynamic typing, you're better off using a dynamically typed language.
But why would you need a container to hold vastly different data types?


because all data wants to fit in and we should not discriminate.
is it nice to say to the int, he can't hang with the char's because he looks different?
Can you give a suscinct, concrete example of where you would find this non-discriminant container useful?

Andy
1
2
3
for (i=0; i<10;i++) {
cout << "HI";
}


opps! I forgot the damn int again!!!

Can you give a suscinct, concrete example of where you would find this non-discriminant container useful?


I cant think of a solid example currently, but the first thing to come to mind is freedom to choose to want to. I am sure the next time i use the availability in python fo multi-type dynamic lists, i will remember to log it.

1
2
3
4
5
6
7
8
9
def func():
    print('executing func')

lister = [lambda:func(), 0]
for i in range(3):
    lister[0]()
    lister[1] += 1
    
print(lister)

I wouldnt write this in python, i would just make it a class with an attribute counter, but it gives options on the table. In c++, the choice is made for me. But it essentially uses a list to house the lambda at index 0, and its count at index 1. But then maybe needing an identifyer or something as a string for index 2. The output is:
1
2
3
4
executing func
executing func
executing func
[<function <lambda> at 0x7f2d2eda4a70>, 3]


This isn't the greatest example, as i could already see a way to get around it in c++, but it just seems like c++ limits options because of it.
Last edited on
dynamic code is flexable and easy to learn. I was able to pick up java script in a couple of weeks and do very impressive things. Here I am in month 9? I think and am still struggling with the basics of C++. I don't mean to hate on C++ because in truth I love it. Its like the coding I did before was baby stepping my way to a man's language.
im on 2 years
@ResidentBiscuit:
Anyways, better to use the language how it was designed, and not try to hack it to make it fit your need.


actually, i think this is called creativity, not hacking.
But why would you need a container to hold vastly different data types?

in my current project, i needed a class member that is publicly accessible for read operations, but private for write operations, when i searched for such thing i saw that programmers didn't give much attention on the topic.
they claimed that this property doesn't have real applications, so it's not important to be included into C++, but i found an application for it.

@OP:
i know that python is an interpreted language, means that the vector isn't created untill the specific declaration line is reached, then it is created with the required type.
in C++, this isn't the case, C++ is a compiled language, this means the compiler determines everything about data types before the execution of the program.
this way, you can't define any object which type is generic.
although as ResidentBiscuit stated about superclass that is the base of all types:
C++/CLI programming defines all data types -including built-in types- as classes derived from a super class System::Object.
maybe you can use this to achieve your goal, program under CLR and define the vector as:
std::vector<System::Object*> v;
this way, the members of the vector can point to any type.
Rechard3 wrote:
actually, i think this is called creativity, not hacking.

It's a very fine line :) I just see it as why fight the language when you can either easily use it for it's strengths, or use a different language. I can sure use a screwdriver to hammer a nail, but why go through the hassle when I have perfect hammer next to me?

Rechard3 wrote:
in my current project, i needed a class member that is publicly accessible for read operations, but private for write operations, when i searched for such thing i saw that programmers didn't give much attention on the topic.

Huh? Private write public read is pretty common... And is a non-issue to implement. I don't even see how this is relevant.
in my current project, i needed a class member that is publicly accessible for read operations, but private for write operations

Private member variable with public getter is how it's done.
@maeriden:
Private member variable with public getter is how it's done.

this would be convenient if you have two,three or up to ten members, my project needed more than that, and public getters are not convenient in such domain.

@ResidentBiscuit:
Huh? Private write public read is pretty common...

i tried to search for the topic, i found so few results, the most convenient one needed a creation of an object to be implemented, which in my case not an option -my class should be abstract with static members-.
if you have a good article, please PM me.

i apologize for OP about this off topic comment, and i hope you had the answer you were looking for.
Rechard3 wrote:
this would be convenient if you have two,three or up to ten members, my project needed more than that, and public getters are not convenient in such domain.

I believe C# has this exact functionality. I have to ask, what situation is this?
Wouldn't it be possible to declare a template class which can take in multiple types ex:

1
2
3
4
5
6
7
8
9
10
11
12
#include <deque>
#include <cstring>
template <class S = std::string, class I = int, class C = char, class B = bool>
class anyType: public std::deque<S>, std::deque<I>, std::deque<C>, std::deque<B> {


};

int main() {
   anyType <> nonDiscriminant_Container;
   return 0;
}


Lol, seems like it should work.
Last edited on
Won't this cause name conflicts inside the class?
In what way? I just wrote that to see if it compiles and it does. What name conflicts do you speak of?
I just wrote that to see if it compiles and it does.


The compiler doesn't generate code for members it doesn't need to. You use no members.
Try the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <deque>
#include <cstring>
template <class S = std::string, class I = int, class C = char, class B = bool>
class anyType: public std::deque<S>, std::deque<I>, std::deque<C>, std::deque<B> {


};

int main() {
   anyType <> nonDiscriminant_Container;

   nonDiscriminant_Container.push_back(0) ;
   nonDiscriminant_Container.size() ;
   return 0;
}


Pages: 12