• c++17


    std::variant<std::string,int,float>;//作为联合体

    using va = std::variant<std::string, int, float>;
    
    va v = 23;
    
    cout << std::get<int>(v);
    va v2 = "this is hello";
    cout << std::get<string>(v2).data();
    std::any a = 2;
    
    //使用std::visit访问,
    
    struct Visitor{
    
      void oeprator()(int m){
    
        cout<<m<<endl;
      }
    
      void operator(string s){
    
        cout<<s.data()<<endl;
    
      }
    
      void operator(float f){
        cout<<f<<endl;  
    
      }
    
    }
    
     
    

      

    std::vaiant可以用作返回值,即std::variant<bool,void*) 如果不符合期望则返回nullptr

    std::any是一个包含任何类型的容器

     1 std::any a = 2; 
     2 //cout << endl << a.type() << endl;
     3 cout << endl << a.type().name() << endl;
     4 a = "string";
     5 
     6 //每次重新赋值 都会变成新的类型名称
     7 cout << endl << a.type().name() << endl;
     8 a = Control["Button"].at(0);
     9 cout << endl << a.type().name() << endl;
    10 a = nullptr;
    11 cout << endl << a.type().name() << endl;
    12 if (nullptr == std::any_cast<std::nullptr_t>(a))
    13 {
    14 cout << "nullptr" << endl;
    15 }
    16 
    17  
  • 相关阅读:
    java中的lamda表达式
    Arrays.sort()中Lambda表达式
    检索中的函数及不同范围的处理
    2014.6.24
    2014.6.23
    第六天培训
    第五天培训
    第四天培训
    第三天培训!
    第二天培训
  • 原文地址:https://www.cnblogs.com/yang131/p/13084613.html
Copyright © 2020-2023  润新知