无意中看到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. linux上安装Docker(非常简单的安装方法)

    Docker的三大核心概念:镜像.容器.仓库 镜像:类似虚拟机的镜像.用俗话说就是安装文件. 容器:类似一个轻量级的沙箱,容器是从镜像创建应用运行实例, 可以将其启动.开始.停止.删除.而这些容器都是 ...

  2. Struts2学习(三)

    一.值栈 1.OGNL表达式 OGNL的概述:对象图导航语言,是一门功能强大的表达式语言. 2.值栈 值栈的概述(ValueStack):是一个接口,实现类OgnlValueStack.是数据的中转站 ...

  3. Mac 远程桌面 ubuntu16.04 unity

    待解决问题: 使用 vnc 远程桌面 ubunt16.04的自带桌面 unity 尝试方法 : 查看了各种方法, 基本都是曲线救国, 安装 gnome 或者 xfce4等其他桌面系统, 而我只想用好看 ...

  4. Linux内存管理 (10)缺页中断处理【转】

    转自:https://www.cnblogs.com/arnoldlu/p/8335475.html 专题:Linux内存管理专题 关键词:数据异常.缺页中断.匿名页面.文件映射页面.写时复制页面.s ...

  5. Python3学习笔记33-正则表达式

    学习文章传送门 正则表达式是用来匹配字符串的.只要符合规则的字符串.就可以认为匹配了.否则,这个字符串不合法. \d:可以匹配一个数字     ‘00\d’可以匹配001不能匹配00A \w:可以匹配 ...

  6. CFtpFileFind例子

    #include <afx.h> #include <afxwin.h> #include <afxinet.h> #include <stdio.h> ...

  7. java接口可以继承多个接口

    接口是常量值和方法定义的集合.接口是一种特殊的抽象类.   java类是单继承的.classB Extends classA java接口可以多继承.Interface3 Extends Interf ...

  8. web@css引入方式,基本选择器,3大特性,高效运行

    4.高效的css 所谓高效就是让浏览器查找更少的元素标签来确定匹配的style元素.      1.不要再ID选择器前使用标签名        解释:ID选择是唯一的,加上标签名相当于画蛇添足了,没必 ...

  9. Uncaught RangeError: Maximum call stack size exceeded

    环境: jquery+bootstrap+bootstrapValidator 问题描述:有个form表单,一点击按钮提交,就会报如题错误.正常应该是去校验表单输入. 解决: 从日志分析来看,报错的起 ...

  10. 随机函数rand()和srand()

    C++中随机函数rand()和srand()的用法 一.rand() 函数名:   rand     功   能:   随机数发生器   用   法:   int rand(void); 所在头文件: ...