How can I create a macro which uses a value multiple times, without copying it?

前端 未结 3 1088
北海茫月
北海茫月 2021-01-05 19:20

I\'d like to create a macro which unpacks a pair into two local variables. I\'d like to not create a copy of the pair if it\'s just a variable, which this would accomplish:<

3条回答
  •  温柔的废话
    2021-01-05 19:27

    What you want is std::tie.

    decltype(p.first) x;
    decltype(p.second) y;
    std::tie(x,y) = p;
    

    If you want, you could even use that to define your macro. Note that this will only work for 2-tuples - if you want 3-tuples or more, you'll need to do it a bit differently. For example, if you have a 3-tuple t:

    decltype(std::get<0>(t)) x;
    decltype(std::get<1>(t)) y;
    decltype(std::get<2>(t)) z;
    std::tie(x,y,z) = t;
    

提交回复
热议问题