1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

構造化束縛before-afrer

Posted at

#構造化束縛における適用前、適用後
C++17において、構造化束縛が追加されました。
とても便利な機能なので是非利用したい。
私としては、忘備録として、この記事を書く。

#before

auto human_bind(){
    struct human{std::string name_; int years_; bool ok_;}
    return human{"elen",21,true};
}

auto a = human();
   std::cout<<a.name_<<","<<a.years_<<","<<a.ok_<<std::endl;

#after

#include<iostream>
#include <tuple>

int main(){
   auto human = {
       std::make_tuple("elen",21,true),
       std::make_tuple("eva",1,false)
   };
    
   for(auto&& [name,years,ok]:human){
      std::cout<<name<<","<<years<<","<<ok<<std::endl;
   }
}

elen,21,1
eva,1,0

#以上
C++20待ち遠しい・・・。。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?