c++17 structure binding test
阿新 • • 發佈:2020-08-27
1 /*test for struct binding*/ 2 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 //名稱空間 可以縮寫為A::B 7 namespace NA::NB 8 { 9 class Data 10 { 11 public: 12 float a; 13 int b; 14 string data; 15 }; 16 Data ff() 17 { 18 //保證宣告時 屬性是可訪問的 19 return { 23.0f,41,"abcdef" }; 20 } 21 } 22 23 int main() 24 { 25 { 26 //這裡可以使A,B,C 可以用來捕獲 27 //作用於當前範圍 28 auto[A, B, C] = NA::NB::ff(); 29 //[[std:cout]] 30 cout << A << endl; 31 cout << B << endl; 32 cout << C << endl;33 } 34 using namespace NA::NB; 35 //無法使用auto 36 Data obj = { 23.0f,41,"abcdef" }; 37 //編譯期斷言 ,如果不滿足條件則會error 38 //static_assert(false,"這個地方編譯不通過"); 39 static_assert(true, "這個地方編譯不通過"); 40 return 0; 41 }