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

前端 未结 3 1100
北海茫月
北海茫月 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:43

    You don't need a macro for this.

    auto p = std::make_pair(2, 3);
    int x, y;
    std::tie(x, y) = p;
    

    If you want references to existing members of a pair:

    auto p = std::make_pair(2, 3);
    auto& x = p.first;
    auto& y = p.second;
    

    That's it.

    Now you can move on to something more challenging/interesting/important.

提交回复
热议问题