Hiding C++ template parameter packs in a tuple

This post has been republished via RSS; it originally appeared at: Microsoft Developer Blogs.

C++11 introduced variadic templates and template parameter packs. Passing template parameter packs around is a bit of a hassle, because the dots "soak up" parameters, which make them hard to pass to other templated types. You also can't "save" parameter packs in another type: One workaround for this is to capture the types into a std::tuple. You can then pass the Tuple around, and if you need to extract the types, you can pull them out of the tuple. My first thought about how to extract the types was to use std::get: This doesn't work because std::get returns a reference: This behavior is presumably so you can modify the tuple: Fortunately, there's another way to extract the type from a tuple: std::tuple_element. This provides a simpler way to extract the last type from a template parameter pack than writing some horrible recursive template metaprogram, which is what some people did instead of hiding the pack inside a tuple.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.