美文网首页
C++奇淫巧技汇总

C++奇淫巧技汇总

作者: Joker64 | 来源:发表于2020-09-25 10:56 被阅读0次

众所周知,Cartographer源码中使用大量c++11特性,为了更加丝滑地读代码,本文将记录源码阅读过程中遇到的奇淫巧技,以了解及学习使用这些特性。

  • constexpr

The keyword constexpr was introduced in C++11 and improved in C++14. It means constant expression. Like const, it can be applied to variables: A compiler error is raised when any code attempts to modify the value. Unlike const, constexpr can also be applied to functions and class constructors. constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time.
Microsoft Docs

至于上文中提到的c++14的改进,是为了避免在编译过程中推断变量(函数的返回值等)的类型甚至是计算变量的值耗费过长时间,而对被constexpr关键字修饰的变量类型或函数返回值进行了限制(必须为Literal types),所谓的literal types包括:

  • void
  • scalar types
  • references
  • Arrays of void, scalar types or references
  • A class that has a trivial destructor, and one or more constexpr constructors that are not move or copy constructors. Additionally, all its non-static data members and base classes must be literal types and not volatile.
  • 匿名命名空间(unnamed namespace)
    顾名思义,匿名命名空间是指定义了命名空间,却不为该命名空间指定名称:
namespace{
  //something
}

对于这样用的原因,网上有很多解释,稍微通俗的解释是:

Having something in an anonymous namespace means it's local to this translation unit (.cpp file and all its includes) this means that if another symbol with the same name is defined elsewhere there will not be a violation of the One Definition Rule (ODR).
All anonymous namespaces in the same file are treated as the same namespace and all anonymous namespaces in different files are distinct.
简言之,就是说匿名命名空间会使命名空间内的所有声明和定义失去外部链接性,但是在当前translation unit内具有链接性,跟static 关键字很像,但是又有所区别。同时可以避免与其他变量的命名冲突。

  • 作用域解析符::
    一段代码解释几乎所有的作用域解析符"::"的用法:
const int x = 5;
namespace foo {
  const int x = 0;
}
int bar() {
  int x = 1;
  return x;
}
struct Meh {
  static const int x = 2;
}
int main() {
  std::cout << x; // => 5
  {
    int x = 4;
    std::cout << x; // => 4
    std::cout << ::x; // => 5, this one looks for x outside the current scope
  }
  std::cout << Meh::x; // => 2, use the definition of x inside the scope of Meh
  std::cout << foo::x; // => 0, use the definition of x inside foo
  std::cout << bar(); // => 1, use the definition of x inside bar (returned by bar)
}

PS:Cartographer源码中经常使用::前不指定命名空间名称的用法。

相关文章

  • C++奇淫巧技汇总

    众所周知,Cartographer源码中使用大量c++11特性,为了更加丝滑地读代码,本文将记录源码阅读过程中遇到...

  • 奇淫巧技

    input type=file 手机浏览器打开相机 input 元素type=file时 ios内核浏览器默认事件...

  • C++奇淫巧技之SFINAE

    SFINAE 技术,即匹配失败不是错误,英文Substitution Failure Is Not An Erro...

  • 开发奇淫巧技

    1.手机在脱离xcode后,重新连接数据线,打开 Debug-attachToProcess,把当前运行进程关联上...

  • JS 奇淫巧技

    作者 | Before Semicolon 译者 | 王强 来源 | 前端之巅 写代码的时候总有一些东西是会重...

  • Mysql的奇淫巧技

    ``` ###查看时区 show variables like '%time_zone%'; ###日期转为时间戳...

  • iOS中实用小技巧

    iOS开发中的★奇淫巧技★ - iOS开发论坛 - 51CTO技术论坛_中国领先的IT技术社区

  • MAC tkinter # If this fails your

    找了半天折腾各种奇淫巧技,最后其实很简单 在mac 命令行执行: brew install python-tk 就...

  • Visual Studio Code 奇淫巧技

    之前一直在用sublime Text 3 做编辑器,无奈一直弹窗让我购买,懒得破解了。根据廖雪峰老师的建议,体验了...

  • Java奇淫巧技之 Lombok

    lombok是一个通过简单的 注解 的形式来简化消除一些必须但显得很臃肿的 Java 代码的工具 简单来说,比如我...

网友评论

      本文标题:C++奇淫巧技汇总

      本文链接:https://www.haomeiwen.com/subject/mboxuktx.html