As an explanation, I'll use a very simple example.
Say I have a structure defined as such:
1 2 3 4
struct data{
char c;
int x;
};
And I want to create a function to print either c or x. However, I don't want to just use a switch or something to figure this out, I want to reference which part I'm outputting.
Now here's the twist
vector<data> dataList;
I need to access element x or c in the vector, but all I pass is the vector and which element, so that I might have something like:
I would give the function the vector, and the function would then loop through the vector (Yes, I know I didn't put a loop) printing element i. However, i could really be either x or c.
So when I say cout << dat[index].i << endl;
That could be either of the following:
I'm pretty sure this cannot be accomplished in C++ or any other strong typed language. I've only ever seen this functionality in scripting languages like Python.
There might be a way to simulate it with templates and some very nasty pointer math... but I'm hesitant to even suggest it because it's so ugly.
EDIT:
Here's my idea, untested. Again I don't recommend this and am just showing it because I find it interesting. I would strongly suggest you reconsider whatever it is you're trying to do and come up with a different way to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <cstdlib> // <- need stdlib for offsetof()
template <typename T>
void yourfunction(const std::vector<data>& d, size_t offset)
{
std::cout << *static_cast<const T*>( static_cast<constchar*>(&d[0]) + offset ) << std::endl;
}
//.....
// usage:
std::vector<data> v;
// assume 'v' populated here
yourfunction<int>(v,offsetof(data,x)); // to print 'x' as an 'int'
yourfunction<char>(v,offsetof(data,c)); // to print 'c' as a 'char'
Well, how would you suggest I sort a vector (End goal) based on the values of certain element inside the structure, so that at one point in the program, I might sort by c, and at another point, I might sort by x? Do I just have to create a function for each element? Even though the code will look the exact same except for it being .c and .x?
Well, in that case, then I'd still have nearly identical code inside of the if statements, which is what I was trying to prevent. It would be cleaner code to just have separate functions, considering that will be using different data types.
Disch, I know that exists. Essentially, I'm trying to not have neatly identical code. BHX just gave a perfect example of what I was hoping to avoid. The functions are identical, save a few letters. But if that's what I have to do, I guess I have to. I was just hoping there was some way to not do it that way.
Disch, I know that exists. Essentially, I'm trying to not have neatly identical code
I'm not sure I see the reason for wanting to avoid this. In my example, the sort routine is literally a single comparison. It's no more redundant to write the sorting lambda than it is to specify which element you want to sort by. It's just more verbose.
The idea was that maybe I could pass something that would say which element i pointed to. It would seem not, and now I know. I appreciated everybody's help and input.
@Daleth:
I may be wrong but do you really need the dummy? Couldn't you just pass the type as a template parameter? Also I think it would be safer to have those operators private and either friend sortbytype or have it as a member.