Giter Club home page Giter Club logo

cpp-syntactic-sugar's Introduction

一些有意思的 CPP 语法糖和 Python 工具

.
├── CRTP                          // Eigen-like 符号表达
├── antiseed-bean        // 机器人“茴香豆”
├── dynamic-arg-template// 模板
├── float-precision      // 最容易错的浮点问题
├── ini-config	               // ini 格式文件读写工具,也可以看作 toml 格式的裁剪
├── log-int-softmax	    // int32 --> uint4 softmax
├── optional                   // 模拟 c++14 `std::optional`
├── range                        // 模拟 `python range` 
├── NCHW4                    // NC4HW4 layout 卷积
├── AOP
├── loan		     // 上海组合贷计算,考虑公积金余额、每月缴存;考虑年冲、月冲;每年提前还款+等本。
├── UB01-no-return
├── UB02-bad-inline
└── github-lark-notifier // github ---> 飞书群 issue/PR 提醒工具

cpp-syntactic-sugar's People

Contributors

megvii-mge avatar tpoisonooo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cpp-syntactic-sugar's Issues

这UB02略坑

在 Linux x64 用 GCC/Clang 简单试了下,结论:

  • inline 修饰的函数定义, 如果没有被当前翻译单元(.cpp)所调用,则不产生符号,就好像不存在过一样
  • 如果对 func.cpp 稍作修改, 调用 func(), 则 func() 生成弱符号
#include "func.h"

inline int func() {
    return 0;
}

int hello()
{
    return func();
}
(base) zz@home% clang++ func.cpp main.cpp -c
(base) zz@home% nm func.o                   
0000000000000000 W _Z4funcv     // func 产生的弱符号
0000000000000000 T _Z5hellov

总之, 当莫名其妙的出现bug时,检查和分析 inline 函数是一个值得检查的checkpoint。

Let the compiler report error for UB01

1. 简单的执行

看了一下 UB01 的例子。应该返回int的函数,但没有返回值:

在 Linux x64 执行,报错为 illegal instruction

(base) zz@home% ./a.out 
zsh: illegal hardware instruction (core dumped)  ./a.out

印象中 android 上会crash,报 “trap”

2. 规避方法

整理 UB 我觉得是很棒的做法,不过记忆 UB 还是有负担的;我的“懒人策略”是用编译选项来提前报告:

clang++ main.cpp -Werror=return-type
(base) zz@home% clang++ main.cpp -Werror=return-type
main.cpp:4:14: error: non-void function does not return a value [-Werror,-Wreturn-type]
int func2() {}
             ^
1 error generated.

图片

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.