Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Representing a Tuple


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Arjan B   Members   -  Reputation: 736

Like
0Likes
Like

Posted 16 November 2009 - 12:27 AM

Hey all! I'm trying to figure out how to represent a database tuple in C++. It can have any amount of attributes and each attribute can have a different type. Something along the lines of this would be nice:
template <class n1, class n2, ... class nN> // N = amount of attributes
struct Tuple {
    n1 attribute1;
    n2 attribute2;
    ...
    nN attributeN;
};

I just can't figure out a way of combining the use of templates and lists for what I want. Any ideas on how this could be achieved? Thank you in advance!

Sponsor:

#2 DevFred   Members   -  Reputation: 836

Like
0Likes
Like

Posted 16 November 2009 - 12:31 AM

The current g++ already supports C++0x's std::tuple which uses variadic templates.

#3 Arjan B   Members   -  Reputation: 736

Like
0Likes
Like

Posted 16 November 2009 - 12:47 AM

Thanks for the quick reply!

It is useless for me to make my own tuple class now, but now I need to create a relation class (which is basically a set of tuples). But that one will need to know which and how many types the tuples have which the relation class is keeping. So I'm pretty much back to my first problem ^^.

#4 gekko   Members   -  Reputation: 478

Like
0Likes
Like

Posted 16 November 2009 - 08:04 PM

As already mentioned, the easy implementation of C++0x's std::tuple uses variadic templates, which is exactly what you want here. However, TR1 also implemented tuple using C++03, so obviously it's possible without variadic templates. Look at an implementation of boost::tuple or std::tr1::tuple and see if you can understand it.

It makes use of typelists (see Modern C++ Design), and meta-programming to traverse the lists. Essentially, you choose the max number of types you want to support (say, 10) and then you make a class taking 10 types which are all defaulted (to a null type). This allows for taking the variable numbers of types (the rest are defaulted) and then the rest is a clever use of typelists.

I needed to do something similar to tuples not too long ago, and needed to learn it for myself. I found boost's docs (and source) the easiest to read and understand. Though, personally I'm more fond with the implementation in VS 2008 SP1. Both are quite helpful, but if you haven't read Modern C++ Design, I'd go over the chapter on typelists first.
-- gekko

#5 Arjan B   Members   -  Reputation: 736

Like
0Likes
Like

Posted 17 November 2009 - 01:44 AM

Thanks!
I'll read through that chapter some time later and after that retry implementing what I want. I'll replay again if I have more questions.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS