16

Hiding C++ template parameter packs in a tuple

 3 years ago
source link: https://devblogs.microsoft.com/oldnewthing/20200529-00/?p=103810
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
quQ73i7.jpg!web

Raymond

May 29th, 2020

C++11 introduced variadic templates and template parameter packs.

template<typename... Args>
struct S;

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:

template<typename... Args>
struct S
{
    // doesn't compile: cannot create a type that
    // is itself a parameter pack
    using TheArgs = Args...;
};

One workaround for this is to capture the types into a std::tuple .

template<typename... Args>
struct S
{
    using Tuple = std::tuple<Args...>;
};

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 :

// code in italics is wrong
template<typename... Args>
struct Traits
{
    using Tuple = std::tuple<Args...>;
    static constexpr auto Size = sizeof...(Args);
    template <std::size_t N>
    <i>using Nth = decltype(std::get<N>(std::declval<Tuple>()));</i>
    using First = Nth<0>;
    using Last  = Nth<Size - 1>
};

This doesn’t work because std::get returns a reference:

void f()
{
 whatis<Traits<int, double>::First>();
}

    call    void whatis<int&&>()

This behavior is presumably so you can modify the tuple:

std::tuple<int> tuple;
std::get<0>(tuple) = 2;

Fortunately, there’s another way to extract the type from a tuple: std::tuple_element .

template<typename... Args>
struct Traits
{
    using Tuple = std::tuple<Args...>;
    template <std::size_t N>
    using Nth = typename std::tuple_element<N, Tuple>::type;
    using First = Nth<0>;
    using Last  = Nth<Size - 1>
};

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.


Recommend

  • 33

    I've resisted adding a module on high-performance computing to this course for a lot of reasons: I think other things are more important, there's enough coverage elsewhere, the software is hard for novices to set up... Bu...

  • 35
    • www.v2ex.com 5 years ago
    • Cache

    tuple 为什么翻译为元组?

    问与答 - @mortonnex -

  • 37

    The TypeScript team recently announced version 3.1 of TypeScript , adding mappable tuple and array types and several other refinemen...

  • 27
    • www.tuicool.com 4 years ago
    • Cache

    Tuple VS ValueTuple

    为什么有此文章 首先要说的是我们公司内部技术框架是用 abp.vnext 框架整合而来的,我们架构师对于 abp 相关的知识都很了然于胸了。并且这个框架的确很优秀,省了我们前期大量基础工作。架构师把主要的架子搭建好了之后,把应用...

  • 38
    • www.banbeichadexiaojiubei.com 3 years ago
    • Cache

    C++11新特性-std::tuple

    1. 引入头文件 #include <tuple> 2. std::tuple初始化 std::tuple<int, std::string, float> t1(10, "Test", 3.14); 这里要注意,不是所有的C++ 11编译器都支持copy-list-initial...

  • 6

    C# 里面很少人知道但很好用的 Tuple 转换小伙伴们都知道有 Tuple 但是很少有小伙伴只有原来从一个类转换为一个 Tuple 的方式如此简洁,在 C# 最新版本里面提供了一组语法糖,可以便捷给任意的类扩展转换为元组的能力 先来看看下面这段有趣的代...

  • 2
    • www.programmerinterview.com 2 years ago
    • Cache

    C++: Function template with more than one type parameter

    How would you create a function template with more than one type parameter? If you don’t know what a function template is, then it would help if you read our quick

  • 0

    New issue P2347 Argument type deduction for non-trailing parameter packs #1055

  • 3
    • www.producthunt.com 1 year ago
    • Cache

    No-Code Template Packs

    No-Code Template PacksFree Airtable, Webflow, & Notion templates to build web appsA free collection of no-code templates from Airtable, Webflow, and Notion. Each "Template Pa...

  • 2

    Iterate Over Parameter Packs in Swift 6.0 March 7, 2024 ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK