get part of std::tuple

前端 未结 7 1189
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 23:09

I have a tuple of unknown size (it\'s template parametr of method)

Is it way to get part of it (I need throw away first element of it)

For example, I have

7条回答
  •  -上瘾入骨i
    2020-12-02 23:57

    Please don't use!

    • This is [most probably] unspecified behaviour. It could stop working at any time.
    • Furthermore, there is a possibility of padding issues (i.e. it may work for int but may fail for your type!).

    See comments for discussion. I'm leaving this answer just for reference.


    Even simpler:

    tuple origin{7,12,42};
    tuple &tail1 = (tuple&)origin;
    tuple &tail2 = (tuple&)origin;
    cout << "tail1: {" << get<0>(tail1) << ", " << get<1>(tail1) << "}" << endl;
    cout << "tail2: {" << get<0>(tail2) << "}" << endl;
    

    I got:

    tail1: {12, 42}
    tail2: {42}
    

    I'm not certain that this is not an unspecified behaviour. Works for me: Fedora 20 and

    ❯ clang --version
    clang version 3.3 (tags/RELEASE_33/final)
    Target: x86_64-redhat-linux-gnu
    Thread model: posix
    ❯ gcc --version
    gcc (GCC) 4.8.2 20131212 (Red Hat 4.8.2-7)
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

    References: article on voidnish.wordpress.com/.

提交回复
热议问题