【C++ 小问答】3:未定义的 `Constexpr` 函数

问 下面哪些在编译期报错(非链接期): 1constexpr int GetVal(); 2constexpr int GetVal() { return 0; } 3constexpr int i = GetVal(); 1constexpr int GetVal(); 2constexpr int i = GetVal(); 1constexpr int GetVal(); 2int i = GetVal(); 1constexpr int GetVal(); 2constexpr int i = GetVal(); 3constexpr int GetVal() { return 0; } 答 将这几段代码分别用 g++ -c …

【C++ 小问答】2:平台对隐式类型转换的影响

问 一个经典问题: 1unsigned int a = ...; 2long b = ...; 3auto c = a + b; // 此处 auto 会推导出什么类型? 答 写了段粗糙的测试代码: 1#include <iostream> 2#include <typeinfo> 3 4int main() 5{ 6 unsigned int var_a = 4; 7 long var_b = 5; 8 auto var_c = var_a + var_b; 9 ::std::cout << "var_a: " << typeid(var_a).name() …

【C++ 小问答】1:模板实例化

问 以下 foo 函数模板被实例化多少次? 1template <typename T> 2void foo(T f) {} 3 4struct Bar { 5 void operator()(bool x) {} 6}; 7 8void f1(bool x) {} 9 10void f2(bool x) {} 11 12int main() { 13 foo(f1); 14 foo(Bar()); 15 foo(f2); 16 foo([](bool x){}); 17 foo([](bool x){}); 18} 答 使用 C++ Insights 生成该代码的编译器视角: 1template …

一个简洁实用的进入 Chroot 环境的脚本

之前在某处看到了这么一份用于进入 Chroot 环境的 Bash 脚本,感觉不错就收藏了。先贴一下原始脚本: 1#!/bin/bash 2 3CHROOT_DIR=$(dirname $(readlink -f $0)) 4 5USAGE=" 6Usage: sh $0 [option] 7 8Warning: This script cannot be used in compile_env 9Options: 10 init init compile_env and mount the required directories 11 no option default to init 12 umount clean up …

Install & Explain SSL Certificates

This is a repost of this answer on Ask Ubuntu, which, primarily showing how to install SSL certificates on CLI, explains as well what happens behind the scene when a certificate verification error occurs1. This work is licensed under the Creative Common Attribute-SharedAlike 3.0 Unported License , as the original post …