无意中看到C++11中的新特性inline namespace, 先附上官方的解释

Inline namespace

The inline namespace mechanism is intended to support library evolution by providing a mechanism that support a form of versioning. Consider:

// file V99.h:
inline namespace V99 {
  void f(int); // does something better than the V98 version
  void f(double); // new feature
  // ...
} // file V98.h:
namespace V98 {
void f(int); // does something
// ...
} // file Mine.h:
namespace Mine {
#include "V99.h"
#include "V98.h"
}

We here have a namespace Mine with both the latest release (V99) and the previous one (V98). If you want to be specific, you can:

#include "Mine.h"
using namespace Mine;
// ...
V98::f(); // old version
V99::f(); // new version
f(); // default version

The point is that the inline specifier makes the declarations from the nested namespace appear exactly as if they had been declared in the enclosing namespace.

This is a very ``static'' and implementer-oriented facility in that the inline specifier has to be placed by the designer of the namespaces -- thus making the choice for all users. It is not possible for a user of Mine to say ``I want the default to be V98 rather than V99.''

  上面的说法大概可以这么理解,当你需要管理多个版本的类库的时候,可以用inline修饰namespace,来达到指定默认版本的目的,例如在以上的例子中 inline namespace V99 在编译的过程中会直接将里面的代码展开,就像如下的表示方式:

namespace Mine {
  void f(int); // does something better than the V98 version
  void f(double); // new feature // ...
 
  // file V98.h:
  namespace V98 {
    void f(int); // does something
    // ...
  }
}

  所以V99里面的方法就相当于默认的方法一样,当我们不指定命名空间直接调用 f(1);  时,就是默认调用V99里面的f();而且这个默认是我们无法去改变的,也就是说我们想调用V98里面的f(),就必须写成这样V98::f(1); 。

inline namespace的更多相关文章

  1. 14.inline与namespace使用

    #include <iostream> using namespace std; namespace all { //inline作用为默认调用 inline namespace V201 ...

  2. 第一章 01 namespace 命名空间

    一.什么是namespace? namesapce是为了防止名字冲突提供的一种控制方式. 当一个程序需要用到很多的库文件的时候,名字冲突有时无法避免.之前的解决思路是使用更长的变量名字,使用不方便. ...

  3. C++——namespace

    scope和namespace scope就是我们常说的作用域,namespace是C++引入的一个关键字.这两种都和作用域有些微妙的联系,下面 引自Global scope vs global na ...

  4. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  5. C++名字空间/C++命名空间

    0.序言 名字空间是C++提供的一种解决符号名字冲突的方法. 一个命令空间是一个作用域,在不同名字空间中命名相同的符号代表不同的实体. 通常,利用定义名字空间的办法,可以使模块划分更加方便,减少模块间 ...

  6. C++11 FAQ中文版--转

    更新至英文版October 3, 2012 译者前言: 经过C++标准委员会的不懈努力,最新的ISO C++标准C++11,也即是原来的C++0x,已经正式发布了.让我们欢迎C++11! 今天获得St ...

  7. Google开发规范

    v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成     头文件   ...

  8. C++Primer笔记(2)

    大型程序一般都是分为多个模块,由多人协作来进行开发的,其中还不可避免的会用到库.而各个模块代码以及库中会定义大量变量,而大量变量的命名,不可避免的会遇见“重名”的问题.“重名”的情况我们称之为命名空间 ...

  9. c++ 17介绍

    作者:hearts zh链接:https://www.zhihu.com/question/32222337/answer/55238928来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...

随机推荐

  1. Handler的postDelayed的实现方法

    暂存,待归纳 https://www.jianshu.com/p/f5f710d55255 https://blog.csdn.net/qingtiantianqing/article/details ...

  2. 6034 Balala Power! (17多校)

    题目大意:给出的字符串,每个字符建立一种与0-25的对应关系.然后每个字符串看成是一个26进制的数.问能获得的数的总和的最大值.(最后对1e9+7取模). 题目思考:把每个字符的贡献值看做一个26进制 ...

  3. vmware常用命令

    控制台桌面执行快捷键ctrl+alt+f2 可以进入命令行

  4. eMMC基础技术1:MMC简介

    [转]http://www.wowotech.net/basic_tech/mmc_sd_sdio_intro.html 1. 前言 熟悉Linux kernel的人都知道,kernel使用MMC s ...

  5. Linux 下的dd命令使用详解(摘录)【转】

    转自:https://www.cnblogs.com/jikexianfeng/p/6103500.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意 ...

  6. C++著名程序库的比较和学习经验

    内容目录:1.C++各大有名库的介绍——C++标准库2.C++各大有名库的介绍——准标准库Boost3.C++各大有名库的介绍——GUI4.C++各大有名库的介绍——网络通信5.C++各大有名库的介绍 ...

  7. Spring MVC全局异常处理

    继承HandlerExceptionResolver接口实现自己的处理方法,如: public class MyHandlerExceptionResolver implements HandlerE ...

  8. java interview

    gitbook address https://dongchuan.gitbooks.io/java-interview-question/content/java/==_equal.html

  9. MySQL--详细查询操作(单表记录查询、多表记录查询(连表查询)、子查询)

    一.单表查询 1.完整的语法顺序(可以不写完整,其次顺序要对) (不分组,且当前表使用聚合函数: 当前表为一组,显示统计结果 ) select distinct [*,查询字段1,查询字段2,表达式, ...

  10. centos6.5中部署Zeppelin并配置账号密码验证

    centos6.5中部署Zeppelin并配置账号密码验证1.安装JavaZeppelin支持的操作系统如下图所示.在安装Zeppelin之前,你需要在部署的服务器上安装Oracle JDK 1.7或 ...