A tricky thing in C++ – Will a member function call by using a raw nullptr crash? Maybe no.

As a Java/Python developer for about 10 years, in my mind, invoking a member function from a raw nullptr, there should have always exceptions, as the following shows.

Maybe you think why I discuss about this naive question. Since recently I found, it’s may not be true for C++ and it depends on how the C++ class defines.

Take a look at the following code written by C++ and execution results.

Surprisingly, the code was executed correctly! It’s tested by VC 2019, GCC 4.8.4/7.3.

I never noticed this but after more time thinking, you may understand the reason. It’s because there is no member field de-referenced in C++ class of “A”.

Let me explain it simply. The function A::hello() will be renamed (there is a full set of naming convention) and compiled into a kind of a C function. And the parameter of pointer of “this” will be added into the function (not that simple, but you can understanding like that). So A::hello() will be compiled to a function like _ZN1A5helloEv(A* this). When you call “a->hello()”, actually, it looks like “_ZN1A5helloEv(a)”. Calling a C function will not cause segment fault, but de-referencing will.

The following code snippet will give you better understanding.

In OOP, this kind of class “A” is not useful. No member fields means a object has no properties. For this case, you should use static function and invoking the function as a class function.

But this tricky behavior is good for us to understand how C++ language works.

Visual Studio 2012 C++里的Lambda

从C++转到Java开发的这几年里,时常感慨Java里没有C++里的函数对象等好用的特性。最近安装了VS2012 Express尝鲜,偶然试了下Lambda,真是不得不惊呼“C++也能这样了!Java何时能支持原生函数对象啊!“,同时吐出几百两血,再想想悲剧的Java模板,再吐出几百两血。 😀

下面就用几个简单的例子说明一下。

一个例子

一个简单例子,很简洁的语法,就可以写出一个lambda表达式,可以赋值给auto变量或function变量。 同时发现在VS2012 Express的IDE已经对这些特性支持的非常好,自动补全也非常给力。

例子中x的类型就是function<int(int)>。

Lambda的基本语法

  1. Lambda introducer或Capture Clauser
  2. Parameter list 声名lambda参数表的
  3. Mutable specification
  4. Exception specification 声名异常
  5. Return type clause 返回值
  6. Lambda body 函数体

其实除了第一项和第三项,其他的部分和写个函数很像。这里就主要讲讲1、3项。

首先一个Lambda表达式可以访问所有上下文中的变量(在一个{}之内的)。Capture Clauser就是用来指定Lambda Body是按值类型访问还是引用类型访问。
举个例子:

如果所有变量都想以值方式访问,可以用[=];都想以引用方式访问可以用[&]。

前面的例子也说明,如果没有参数表,可以省略。

mutable关键字是和by value的访问方式有关。当我们以by value方式访问变量时,默认是不能对变量进行赋值的。但当我们声明为mutable的lambda时,就可以了。注意,这里修改的也仅仅是一个拷贝而不是值本身。

和STL算法联用

 

Lambda的加入方便了C++写出简洁的代码,可以和STL里很多算法联用,非常方便,功能强大。当然也不仅限于VS2012,新版的LLVM和GCC对lambda也支持的很好!

Java 8里会有这些吗?Who knows?

References

  1. Examples of Lambda Expressions
  2. Lambda Expression Syntax

Accelerate the Compiling Speed by Using Pre-compiled Header

前言

这是2009年我在项目组做的一个改进。项目大约有80万行的代码(现在已过百万),编译非常慢。当时大家苦不堪言,工作效率很低。预编译头文件可以大幅提高编译效率。在一些尝试之后,我写了一个Python脚本,把工程中的常用头文件提取出来做成预编译头,也成为组里为数不多的,把全部代码Check out的工程师。你可以在这找到Python的脚本。

Introduction

This article is trying to explain why and how to use pre-compiled header file to accelerate our projects compiling(not including linking) speed.

Motivation

In our daily work, we modify code for fixing bugs or implementing new features, and then build new binary file and test it. The building process usually costs between 3~20 minutes. And sometimes we have to sync other colleagues’ code, the building process is much longer. So accelerating building speed is very important to improve our efficiency. Building process contains two steps. One is to compile source files and generate object files, the other is to link all object files together and generate binary file. This artical is focus on accelerating the first step.

How to use pre-compiled header

Right now many compilers, including VC++ compiler and GCC, supply a technology called pre-compiled header, which can greatly improve the compiling efficiency. Assumed there is a structure of source files.

A.cpp incudes the wx/wx.h and also does B.cpp and C.cpp. When compiling A.cpp, B.cpp and C.cpp, the compiler will parse the wx/wx.h three times. wx/wx.h is a quitely-often-used header file in our project. And there are hundreds of source files in our projects. So the wx/wx.h will be parsed hundreds of times. And there are many often-used header files like header files in std(Standard Template Library) and wxWidgets.

Pre-compiled header technology uses a common header file to generated pre-compiled header file(.pch using by VC++, .gch using by GCC). The common header file contains as many as often-used header files and we seldom change it. The former structure should be changed like the following schematic.

Now the header file called “stdwx.h” contains other often-used header files. Since the header file can not be compiled, stdwx.cpp is used to generate the pre-compiled header file. And we need to change the project build configuration like below.

For VC++

For whole project,

NOTE: Actually, we can change the configuration for every single source(.cpp) file using the same method. But project-level setting is much more convenient, it can change the other source file configuration, except for those already have their own setting of precompiled header.

For stdwx.cpp,

NOTE:  stdwx.cpp is special, since the header(.h) file can not be compiled by compiler. We need a cpp file to let the compiler to compile. The content of stdwx.cpp is very simple, just include stdwx.h. So compiler will use stdwx.cpp to generate the precompiled header.

For GCC

GCC supports a compile option called -x c++-header (for cpp) and c-header(for c), which used to generate the precompiled header file. Please see the following statements in makefiles.

After that, when compiler compiles the A.cpp, B.cpp and C.cpp, the stdwx.h will be parsed only once by compiling stdwx.cpp. That how the time is saved!

NOTE: The source files using pre-compiled header should include “stdwx.h” at the first line of the source, or there will be compiling errors!

Test Result

The test result is very exciting! Project has 80,000 LoC.

With Pre-compiled Header Without Pre-compiled Header
Re-build Time(NOT including linking time) 30~35min 130min+

The re-build time with pre-compiled head is only about 23% of that of without pre-compiled header project. In other words, the compiling efficiency is improved over 70%.

关于如何用C++实现一个不可以被继承的类

很早以前在某个笔试题集里看到了这个题目,MS是Adobe公司出的。当时第一反应要用到什么私用化构造函数,友元之类的技术,但后来实现的时候却发现更多的问题,现在已经一一搞明白了。今天记在这,总结一下。

主要的代码:

几个要点:

  1. 在NoDeriveBase这个类里是不能直接写,friend class T的,编译器(gcc)会报错。所以要用Type2Type的技术,Loki库里常用的技术。
  2. 为什么A要虚继承自NoDeriveBase?原因是A虚继承后,B再继承A时,B要直接访问虚基类的构造函数,而不是直接通过A的构造再访问NoDeriveBase的构造。所以,如果不虚继承的话,B是可以继承A的。
  3. 这段代码会编译出错。B是不可以继承A的。

终于都明白了! 🙂