Skip to content

Latest commit

 

History

History
executable file
·
52 lines (43 loc) · 1.18 KB

02-types.md

File metadata and controls

executable file
·
52 lines (43 loc) · 1.18 KB

代码总览

提供了一些枚举变量和静态常量. 用于一些标识符?

功能/知识

1. 枚举变量Relation

  • enum可以指定底层类型, 如下. 注意这个std::uint8_t, 是强制底层类型.
    enum Relation : std::uint8_t {
      NO_RELATION = 0,
      DIFF_HOST,  // different host
      DIFF_PROC,  // same host, but different process
      SAME_PROC,  // same process
    };
  • 又如:
    #include <iostream>
    #include <string>
    #include <typeinfo>
    enum class Relation1 : std::uint8_t {
      NO_RELATION = 0,
      DIFF_HOST, 
      DIFF_PROC,  
      SAME_PROC,  
    };
    enum class Relation2{
      NO_RELATION = 0,
      DIFF_HOST,  
      DIFF_PROC,  
      SAME_PROC,  
    };
    int main(int argc, char* argv[]) {
      std::cout << typeid(std::underlying_type<Relation1>::type).name() << std::endl;
      std::cout << typeid(std::underlying_type<Relation2>::type).name() << std::endl;
    }
    它会输出:
    h
    i
    h应该是usigned int,i应该是int. 可能是编译器简写了.
  • 这个博客讲枚举变量的值得作用域的, 讲得不错. 注意enum classenum struct. https://blog.csdn.net/datase/article/details/82773937