Implementing a variant type from scratch in C++

{38 Comments}

When programming in C++ we sometimes need heterogeneous containers that can hold more than one type of value (though not more than one at the same time). If our container needs only contain basic types and Plain Old Data Structures, the solution is simple:

union simple_variant_1 {
   int as_integer;
   bool as_bool;
   float as_float
}

If we want to know which type is stored inside, we can make a tagged union, something along the lines of:

struct simple_variant_2 {
   enum {t_int, t_bool, t_float} type_id;
   union {
      int as_integer;
      bool as_bool;
      float as_float
   }
}

From the efficiency standpoint, this is probably almost an optimal solution. An union will only consume as much space as the largest of its member types. If we try to access the as_float member variable when the actual value stored is int, we will see implementation-dependent gibberish values but that’s OK because we know we are accessing the wring type. Likewise, basic types do not need to be initialized or finalized – they can safely contain gibberish and after we are done using them, they don’t need to do free memory, clean up global state etc.

The problem of course arises, when we want to use “smart” objects, such as std::string, std::vector, … in our union. These types implement custom constructors, copy and move operators, destructors, … The internal structure of std::string, for example, contains a pointer to the string data. If this structure is not properly initialized, it will point to a random location in memory, causing a segmentation fault the first time we access it. If it is not finalized, the memory that it allocated will not be freed thus causing memory leaks.

This is exactly the reason why non-basic types were disallowed in C++ unions. When the union is created, there is simply no means of knowing which of its members should be initialized and when it goes out of scope, the compiler cannot know on which type to perform the destructor. In C++11, the rules have been relaxed, but this requires us to take the initializing/finalizing part into our own hands.

This is how a variant type implementation can look like. We use the rarely-used placement new syntax that allows us to decouple the memory acquisition and object initialization that the new operator provides, by performing only the initialization part on a memory that we already have. In the same manner, we have to manually call the destructor, because free also attempts to deallocate memory.

struct variant {
   enum variant_type {t_int, t_string};
   variant_type type_;
   union {
      int as_integer;
      string as_string;
   };
   variant(variant_type type) : type_(type) {
       if (type == t_string)
           new (&as_string) string();
       else
           new (&as_integer) int();
   }
   ~variant() {
       if (type_ == t_string)
           as_string.~string();
       else
           as_integer.~int();
   }
};

Of course in the above example, we notice that new (&int) as_integer and as_integer.~int() are completely redundant, because integers do not need neither initialization nor finalization, so their constructors and destructors are NOP. This is included for consistency once we get to the point of using templates for generic variant types.

Note: In fact the above code does not even compile on GCC 4.8 and Clang 3.4, but when we use templates to refer to int with a generic template parameter, everything works as expected.

The above code works just well but this approach does not lend itself to implement generic templated types. What we would like is something, that can be used like this:

variant<int, float, string> var;
var.as<string>() = "Hello world!";
cout << var.as<string>(); // Prints Hello World
var.as<int>() = 42;
cout << var.as<int>(); // Prints 42
var.as<float>() = 3.14;
cout << var.as<float>(); // Prints 3.14

In the following section we will build the fully-templated variant type from scratch.

Basic construction

We begin the construction by evaluating the basic layout of our variant type. The approach with unions works well, when we have a hard-coded set of types to choose from, but not so much when we have a templated-type.

We start by setting the basic layout to something like this:

struct variant {
   type type_id;
   char data[enough];
}

This of course raises the question of “How much is enough?”. Since we need every object to fit into the container, it must be at least as big as the largest of the member types.
This is the time to introduce a helper object and some variadic template magic:

template<typename F, typename... Ts>
struct variant_helper {
  static const size_t size = 
    sizeof(F) > variant_helper<Ts...>::size ? sizeof(F) : variant_helper<Ts...>::size;
};

template<typename F>
struct variant_helper<F>  {
	static const size_t size = sizeof(F);
};

We will use the helper object for everything that will require a template specialization for the case of one single member type so as to avoid the code duplication in the main variant class.

This allows us to define:

template<size_t size>
class raw_data { char data[size]; };

template<typename... Ts>
struct variant {
  typedef variant_helper<Ts...> helper;

  type type_id;
  raw_data<helper::size> data;
}

We use the wrapper class raw_data to work around the awkwardness and inconsistency of C-style arrays. This also allows us to easily change the definition if we are programming for a platform where say, sizeof(char) != 1.

Edit (10/11/2013): as some have pointed out, this implementation of raw_data will lead to alignment issues on certain platforms, which can be alleviated by declaring the container type a union of all the variant member types. C++11 provides a very nice container type that works with variadic templates, that we can directly use in this code without any other changes. We can also use aligned_storage. The full source code has been updated.

Type identification

We need a way to create a mapping between member types and values that will be stored in the type_id variable. We will use C++’s very handy RTTI utilities. This requires that the program be compiled with RTTI data and in theory it would be possible to implement this without using it but we would have to manually create type traits for every type that we would like to use, like this:

template<typename T> struct type_traits;
template<> struct type_traits<int> { static const size_t id = 0; };
template<> struct type_traits<string> { static const size_t id = 1; };
template<> struct type_traits<float> { static const size_t id = 2; };
/* ... */

This is useful in some cases, for example if we want to have precise control over the representation of the stored type ID, but very cumbersome when writing a general-purpose container. If we take a look at the std::type_info, we see that typeid(T).hash_code() is guaranteed to return a unique size_t-typed value for every different type, making it ideal for our purposes.

It is also possible to have the types identified sequentially with some template magic, but we would like to have global type IDs, to allow for compatibility between the variant type instantiated with different sets of member types in a possible future expansion.

Destructor

We will now look at the implementation of the destructor. The destructor has to go through every member type and check if it’s ID matches the ID stored in the object. If this is the case, it has to cast the data to this member type and invoke its destructor.
We create an external routine called destroy in the helper class, so that we don’t have to specialize the main template:

template<typename F, typename... Ts>
struct variant_helper {
  /* ... */
  inline static void destroy(size_t id, void * data) 
  {
    if (id == typeid(F).hash_code())
      reinterpret_cast<F*>(data)->~F();
    else
      variant_helper<Ts...>::destroy(id, data);
  }
  /* ... */
};

template<typename F>
struct variant_helper<F>  {
  /* ... */
  inline static void destroy(size_t id, void * data) 
  {
    if (id == typeid(F).hash_code())
      reinterpret_cast<F*>(data)->~F();		
  }
  /* ... */
};

template<typename... Ts>
struct variant {
private:
  typedef variant_helper<Ts...> helper;

  size_t type_id;
  raw_data<helper::size> data;
public:	
  /* ... */
  ~variant() { helper::destroy(type_id, &data); }
  /* ... */
};

Initialization of objects

Contrary to Boost.Variant’s Never-Empty guarantee, our variant type will allow the uninitialized state – the “empty container” – because it greatly simplifies matters. Ergo, we need only make the default constructor:

template<typename... Ts>
struct variant {
  static inline size_t invalid_type() {
    return typeid(void).hash_code();
  }
  /* ... */
  variant() : type_id(invalid_type()) {	}
};

Despite what we outlined in the introduction, we will have two functions to access the member types – set() and get().
set() will perfectly forward all of its parameters directly to T‘s constructor, whereas get() will throw an error if we try to access the type that is not currently contained within.

template<typename Ts>
struct variant {
  /* ... */
  template<typename T, typename... Args>
  void variant::set(Args&&... args)
  {
    // We must first destroy what is currently contained within
    helper::destroy(type_id, &data);		

    new (&data) T(std::forward<Args>(args)...);
    type_id = typeid(T).hash_code();
  }
 

  template<typename T>
  T& get()
  {
    if (type_id == typeid(T).hash_code())
      return *reinterpret_cast<T*>(&data);
    else
      throw std::bad_cast();
  } 	
  /* ... */
}

Copy and move semantics

At this point, the variant type is already usable, but its functionality is severely limited since it does not implement move/copy semantics, so we cannot use it in STL containers. Actually, it is worse. The implicitely defined copy constructor will just copy the contents of data, so when both copies fall out of scope, the object therein contained will be finalized twice, which will probably lead to a crash. It is therefore imperative to either implement custom constructors/operators or mark the object as non-copyable/non-moveable.

The move constructor is very simple, since we only need to steal the resources and invalidate the old instance:

variant(variant<Ts...>&& old) : type_id(old.type_id), data(old.data)
{
  old.type_id = invalid_type();
} 

The copy constructor involves some more work, since it has to invoke the suitable copy constructor of the member type of the object contained in the original container.

template<typename F, typename... Ts>
struct variant_helper {
  /* ... */
  inline static void copy(size_t old_t, const void * old_v, void * new_v)
  {
    if (old_t == typeid(F).hash_code())
      new (new_v) F(*reinterpret_cast<const F*>(old_v));
    else
      variant_helper<Ts...>::copy(old_t, old_v, new_v);		
  }
};

template<typename F>
struct variant_helper<F>  {
  /* ... */
  inline static void copy(size_t old_t, const void * old_v, void * new_v)
  {
    if (old_t == typeid(F).hash_code())
      new (new_v) F(*reinterpret_cast<const F*>(old_v));
  }
};

struct variant {
  /* ... */
  variant(const variant<Ts...>& old) : type_id(old.type_id)
  {
    // We need to do a "deep" copy, that is - invoke the copy constructor of the contained type.
    helper::copy(old.type_id, &old.data, &data);
  }
  ...
}

The last thing we need are the corresponding operator= implementations, everything as standard and some helper functions:

variant<Ts...>& operator= (variant<Ts...>&& old)
{
  // We steal the data and invalidate the old value
  data = old.data;
  old.type_id = invalid_type();
	
  return *this;
}

variant<Ts...>& operator= (variant<Ts...> old)
{
  // Note the call-by-value above
  std::swap(data, old.data);
  std::swap(type_id, old.type_id);
		
  return *this;
}	

// ----------------------------------------- //

template<typename T>
void is() {
  return (type_id == typeid(T).hash_code());
}

void valid() {
  return (type_id != invalid_type());
}

Conclusion

This implementation does not use dynamic memory management/heap storage, so it should be quite efficient. It’s goal is to be simple to understand but may not be so robust as heavy-duty implementations, such as Boost.Variant. It uses fancy C++11 features, so it will probably not work on older compilers The full source code is available here.

38 Comments…

 Share your views
  1. This doesn’t take into account alignment. You should be using std::aligned_storage in place of raw_data.

  2. There are alignment issues. char buffer[size]; for placement new of T is inadequate. This is why we you see code only using placement new of T onto std::aligned_storage which has the right alignment for T. Look up std::aligned_storage (added in C++11), or boost::aligned_storage for more information.

  3. Your move constructor is not proper. Consider for example a type T that is a class that contains pointers to its other member elements.
    You will have to call the object’s move constructor instead of just copying raw data. If you simply copy the raw data, your now relocated object will contain invalid pointers.

    This goes for the move-assign, copy-construct and copy-assign methods as well.

    • Tibor Djurica Potpara October 13, 2013 at 19:32

      I updated the code to use proper move semantics, though this comes with an overhead, since it is much more effective to just copy the data. I wonder what is the actual occurrence of types with absolute pointers to their members.

      • Actually, there’s no reason why a move constructor in generic case would not be as fast as a memcpy (other than a suboptimal compiler that insists on e.g. copying field by field)…

        • Tibor Djurica Potpara October 14, 2013 at 16:27

          It has to perform the move constructor of the type stored within. To do so, it has to do at least one extra comparison, but yeah, I don’t think that this is a problem either. Variant types are just not without tradeoffs.

  4. Alessandro Vergani October 15, 2013 at 12:42

    In the article, the move operator= is missing the setting of the type_id member.

  5. I have to thank you for the efforts you hav put in penning this website.
    I’m hoping to see the same high-grade content from you later on as well.
    In truth, your creative writing abilities has motivated me to get
    my very own site now ;)

  6. My family members always say that I am killing my time here at
    net, except I know I am getting know-how daily by reading
    such fastidious posts.

  7. Hey! This is kind of off topic but I need some help from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure things
    out pretty fast. I’m thinking about creating my own but I’m not sure
    where to begin. Do you have any ideas or suggestions?
    Many thanks

  8. What you published was very reasonable. But, think about this, what if you added
    a little information? I mean, I don’t want to tell you how to run your blog, but suppose you added a title that makes people desire more?
    I mean Implementing a variant type from scratch in C++ – Ojdip.net is kinda plain. You could look at Yahoo’s home page and note how they create article titles to grab viewers
    to open the links. You might try adding a video or a related pic or two to get people interested
    about what you’ve got to say. In my opinion, it would bring your blog a little bit more interesting.

  9. Hmm is anyone else having problems with the pictures on this blog loading?

    I’m trying to determine if its a problem
    on my end or if it’s the blog. Any responses would be greatly appreciated.

  10. Great post. I’m dealing with some of these issues as
    well..

  11. What’s up friends, its great article about cultureand fully explained,
    keep it up all the time.

  12. Hello There. I found your blog using msn. This is a really well written article.

    I will be sure to bookmark it and come back to read more of your useful info.
    Thanks for the post. I’ll certainly return.

  13. This is my first time pay a quick visit at here and i am really pleassant to read
    all at one place.

  14. Greetings from Idaho! I’m bored at work so I decided to browse your website on my iphone during
    lunch break. I really like the knowledge you provide
    here and can’t wait to take a look when I get home.
    I’m surprised at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, very good blog!

  15. I’ve read several excellent stuff here. Certainly price bookmarking
    for revisiting. I wonder how so much attempt you put to create the sort of excellent informative website.

  16. After looking into a handful of the blog posts on your website, I seriously like your way of blogging.

    I bookmarked it to my bookmark website list and will be
    checking back soon. Please check out my website as well and let me know your opinion.

  17. An outstanding share! I’ve just forwarded this
    onto a coworker who has been conducting a little homework on this.

    And he actually bought me dinner due to the fact that I found it for him…
    lol. So allow me to reword this…. Thanks for the
    meal!! But yeah, thanx for spending some time to discuss
    this issue here on your web page.

  18. I am now not sure where you are getting your information, but good topic.
    I must spend some time finding out much more or figuring out more.
    Thanks for magnificent info I was in search of this information for my mission.

  19. Hi there! Would you mind if I share your blog with my facebook group?
    There’s a lot of people that I think would really appreciate your content.
    Please let me know. Thank you

  20. Excellent blog here! Also your web site loads up very
    fast! What web host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as fast as yours lol

  21. Heya superb website! Does running a blog similar to this take a lot
    of work? I have virtually no expertise in programming
    but I had been hoping to start my own blog soon. Anyhow, if
    you have any ideas or tips for new blog owners please
    share. I know this is off subject but I simply had to ask.

    Thanks a lot!

  22. Hi there, just became alert to your blog through Google, and found that
    it is truly informative. I am gonna watch out for brussels.
    I will appreciate if you continue this in future.
    Many people will be benefited from your writing. Cheers!

  23. When I originally commented I appear to have
    clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I receive four emails with the exact same comment.
    Perhaps there is a means you are able to remove me from that service?
    Thanks!

  24. This is very interesting, You are a very skilled blogger.

    I have joined your rss feed and look forward to seeking more of your
    excellent post. Also, I have shared your site in my social networks!

  25. It can be found occasionally with using the “Sculptor’s Workbench.
    Ametrine is a very durable gemstone suited for everyday wear.
    Color television sets emit more EMF than do black and white
    sets, yet even the black and white TVs have been associated with 2.

  26. hi!,I love your writing very so much! share we communicate extra approximately your post on AOL?

    I require a specialist on this house to solve my problem.
    May be that’s you! Looking forward to see you.

  27. First of all I want to say awesome blog! I had a quick
    question which I’d like to ask if you do not mind. I was interested to know
    how you center yourself and clear your head before writing.
    I’ve had difficulty clearing my thoughts in getting my thoughts out.
    I truly do take pleasure in writing but it just seems like the first 10 to 15 minutes are lost just trying to figure out
    how to begin. Any suggestions or hints? Many thanks!

  28. Hi Dear, are you genuinely visiting this website daily,
    if so then you will absolutely take fastidious know-how.

  29. I’m not sure why but this site is loading very slow for me.

    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  30. If you want to grow your know-how just keep visiting this site
    and be updated with the hottest information posted here.

  31. I really like reading a post that will make men and women think.
    Also, many thanks for allowing for me to comment!

Leave a Comment

Your email address will not be published.