Creating generic linked list to contain ALL types.

Hey I started this a while ago and well ran into a wall because of one major problem. That problem is that how exactly do I know the type of the next generic node? By that I mean how do I convert my conversion pointers to the correct node/class type?

This is what I have so far and well...It doesn't work at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once

#ifndef GENERIC_CONTAINER_H
#define GENERIC_CONTAINER_H

#define NO_LOC -1

class generic_container {
private:
	struct conversion {};
	template<typename storage> struct data : public conversion {
		conversion* next;
		storage info;

		using TYPE_DATA = storage;
	};
	static conversion* head;
	conversion* tail;

	size_t size;

	typedef size_t index;
public:
	generic_container();
	~generic_container();
	template<typename data_type> void insert(data_type add) {
		if (head == nullptr) {
			head = new data<data_type>;
			head->info = add;
			tail = dynamic_cast<data>(head);
		}
		else {
			
		}
	}
	decltype(auto) at(index location) {
		
		if (location < size) {
			conversion* cycle = head;
			for (index value = 0; value < location; value++) {
				return 1;
			}
		}
	}
	template <typename data_request> index find(data_request value) {
		while (index = 0; index < size; index++) {
			if (typeid(data_request) == typeid(cycle->info)) {
				if (value == cycle->info) {

				}
			}
			cycle = cycle->next;

		}
		return NO_LOC;
	}
	void clear() {

	}
};
#endif 
C++ is not designed for this. You could easily do this in a weakly typed language, but you'd almost never want to.

Why do you want to throw away C++'s strong type system? If you actually have a valid reason, use something like boost::any inside a std::vector or similar.
Last edited on
I understand that C++ was purposely made with a strong type system but I was just planning on creating a generic linked list as a useful tools for rare occasions and also as a challenge/neat thing to have. I guess that isn't an extremely good reason but yeah. By C++ is not designed for this you aren't implying that this is impossible right but rather it will be difficult? If so any suggestions?
My suggestion, look at the way boost::any does it - by using a helper base class and template class that derives from it via the constructor. You are trying to combine the concepts of 'wrapper around any type' and 'list of objects' - it will be easier to split them apart into separate concepts and then use them together after that. It is seldom necessary to write your own container or list class.
Topic archived. No new replies allowed.