前言

先看一段代码

#ifndef  _INLINE_H
#define _INLINE_H template<typename T>
static inline
T my_max(T a, T b)
{
a *= 2;
b /= 3;
return (a>b) ? a : b; //找出最大值
} #endif

代码本身逻辑很简单,无外乎简单的找出两个T类型变量中大者。

这里有几个关键字的用法很值得深究,特此记录下感想。

inline

简单的理解inline就是,他只有带参宏的优点,没有带参宏的缺点。但实际情况,这可能仅仅是Programmer的一厢情愿。因为啥? inline和register的行为很像,正确的理解方式是,这两个关键字是programmer对compiler的一种建议。至于你的建议会不会被compiler所接纳,那是compiler的事,我们不应该对compiler有任何假设。因此上面代码中是对然你加了inline,最终效果可能和不加inline的normal function一样。

static

要想理解此处static的作用,首先想像我们经常遇到的重复定义error。软件开发中遇到的重复定义error,99%都是来自于global空间被“污染”。为啥这么说?对于文件作用域内的variable or function,重复定义情况你是很容易发现的,都写在一个文件中,打眼一看就知道有没有冲突。然而实际开发中,多人协作开发,global空间有啥东西谁也不清楚。对于那些拍脑子乱加global variable or function 的人更是如此,可能他自己“污染”了global空间,自己都不知道。

想想哪些地方可能会导致global空间被“污染”,嗯....头文件(.h)和.c文件

.h文件

对于在.h里面的强符号,应该始终秉持这样一种观点:尽可能将header files封锁在include他的单个.c文件。除非对那些必须要共享的variable or function,其余强符号最好加上static。虽然header guard帮我们避免了一份头文件多次包含的情况,确保整个头文件在最终可执行文件中只有一份。但是header guard无法保证header files中的强符号与其他人.h  or  .c文件中的强符号发生重复定义风险。

.c文件 

对于.c中的强符号,如果不想被他人使用 或者 可能给他人带来风险,最好加上static,将其锁死在文件作用域。

软件开发是个多人合作活动,对于编码问题上的风险,我们应该将其扼杀在萌芽中。一个良好的编码规范无疑是重要的。核心代码必须有企业自己把握,业务代码(简单扩充函数)完全可以外包出去,外包出去的代码质量由公司内部人员把控。这样可以进一步缩减人力成本。

variable hides

#include <stdio.h>

int var = 20;
int main()
{
int var = var;
printf("%d\n", var);
return 0;
}

variable hides很好理解,The local variable always hides a global one of the same name as soon as it's declared.

那么换成function hides呢?

我们知道C不支持overloading,C++支持overloading。C++下最终识别到的函数名字类似于这样:

那有没有这种可能?

某个.c内的static function 和 某个 global function在compiler那一侧解析的名字完全一致,那不就会存在类似于variable hides的情况了吗?虽然这种情况概率比较低,但是面对百万行代码的时候还是难免不会遇到。

事实上,这个问题完全是多虑了。

之所以会想到这个奇怪的问题,其根源是对作用域问题的思考。上层上讲就是谁的作用域有效,低层上函数名就是个地址,地址值都不一样。因此最终翻译成二进制代码的时候是不会出现function hides的情况的。

关于header file、static、inline、variable hides的一点感想的更多相关文章

  1. About Why Inline Member Function Should Defined in The Header File

    About why inline member function should defined in the header file. It is legal to specify inline on ...

  2. Header File Dependencies

    [Header File Dependencies] 什么时候可以用前置声明替代include? 1.当 declare/define pointer&reference 时. 2.当 dec ...

  3. 原文:I don’t want to see another “using namespace xxx;” in a header file ever again

    http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers http://stackoverflow.com/que ...

  4. C1853 编译器错误:fatal error C1853: 'pjtname.pch' precompiled header file is from a previous

    转载:https://www.cnblogs.com/emanlee/archive/2010/10/16/1852998.html 用VC++ 2008 编写C语言程序,编译出现错误: 预编译头文件 ...

  5. 【iOS】The differences between Class Extension and Header File 类扩展与头文件的区别

    . As the name suggests, they extend the class. A class continuation is another name. The class exten ...

  6. static inline

    今天看到了这样一段代码, static inline BOOL IsEmpty(id thing) { return thing == nil || [thing isEqual:[NSNull nu ...

  7. static local variable

    Putting the keyword static in front of a local variable declaration creates a special type of variab ...

  8. auto make System.map to C header file

    #!/bin/bash # auto make System.map to C header file # 说明: # 该脚本主要是将Linux内核生成的System.map文件中的符号.地址存入结构 ...

  9. c++预编译问题:fatal error C1083: Cannot open precompiled header file: 'Debug/DllTest.pch': No such file or d

    1)单独编译StdAfx.cpp 2)编译所有(即按Ctrl+F7) 这时因为该模块没有包括预编译头文件“stdafx.h”的缘故.VC用一个stdafx.cpp包含头文件stdafx.h,然后在st ...

随机推荐

  1. GPL & LGPL

    Reference https://www.cnblogs.com/findumars/p/3556883.html GPL 我 们很熟悉的Linux就是采用了GPL.GPL协议和BSD, Apach ...

  2. CardUtil算出当前身份证持有者的性别和年龄

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util ...

  3. (原)关于音频onset detection算法的阅读

    Orgin:Using Audio Onset Detection Algorithms 本文档只记录了部分的内容,主要以aubio相关内容为主,并非整个文档的完整内容,记录人:lihaiping16 ...

  4. PMP 第7章错题总结

    这一章全是公式,是计算量最大的一章 1.进度安排的灵活性是由总时差决定的2.三点估算通过考虑估算中的不确定性和风险,可以提高活动持续时间估算的准确性3.快速跟进是一种进度压缩技术,将正常情况下按顺序进 ...

  5. Redis 分布式锁的正确实现方式(Java版)[转]

    本文来源: https://www.cnblogs.com/linjiqin/p/8003838.html 前言 分布式锁一般有三种实现方式: 数据库乐观锁: 基于Redis的分布式锁: 基于ZooK ...

  6. Maya编程——沿Curve绘制圆柱

    操作流程: 1. VS运行代码,生成插件 2. 打开Maya绘制曲线,加载插件 3. 选中绘制的曲线,运行插件 Posts1.0 代码: #include <maya/MSimple.h> ...

  7. Spring Boot接入 apollo 后启动 dubbo 报错

    原文地址:https://xobo.org/spring-boot-apollo-dubbo-xml-error/ 某Spring Boot项目接入 apollo 后启动 dubbo 报错Caused ...

  8. [转]Ubuntu18.04隐藏顶栏与侧栏

    链接地址:https://blog.csdn.net/liu_jiangwen/article/details/85215297

  9. 工具分享:excel2json,将Excel表格转换为JSON

    此次分享的是github上的一个开源小工具:excel2json,用于把Excel表转换成json对象,并保存到一个文本文件. 项目地址:https://github.com/neil3d/excel ...

  10. 精通react之react-router4源码分析(100代码实现router功能)

    1.react-router4 是一个 react 组件 通过和 location / histroy 结合,来显示页面不同URL对应显示不同的组件 其中包含了三种路由.hash / boswer 等 ...