Parallel assignment in C++

前端 未结 6 918
醉酒成梦
醉酒成梦 2021-01-18 13:53

Is there any way of doing parallel assignment in C++? Currently, the below compiles (with warnings)

#include  

int main() { 
  int a = 4;
           


        
6条回答
  •  Happy的楠姐
    2021-01-18 14:08

    Or Perl. But no, it's not possible (as far as I'm aware), you need to use a temporary variable, as in:

    int a = 4;
    int b = 5;
    
    {
        int tmp = a;
        a = b;
        b = tmp;
    }
    

    FYI, internally those languages (or Perl atleast) create a temporary list { a, b }, then assign the list to the two variables. In other words, this is atleast as performant, if only a little more messy.

提交回复
热议问题