找回密码
 立即注册
首页 业界区 业界 阿里P7,竟问这么简单的题目?

阿里P7,竟问这么简单的题目?

梁丘眉 2025-6-21 13:31:48
今天面试阿里后端开发,
● 面试官提问:谈谈你对 const 理解
●  我这样回答的: const 仅仅表示变量不能修改,太简答了,我早就知道了。
● 面试官回复:你觉得 这样能面过 P7 ,百万年薪岗位吗。
痛定思痛,今天,聊聊比较重要性能优化的思路:
● 编译期优化
● 从 cpu 角度理解 编译期优化
一. 在项目中带来什么收益,why
1.1 编译期计算
● 所有比较在编译期完成,零运行时开销。
● 例如  if constexpr  std::is_same_v (先别走看,不同特性,有着同样原理)
● 更多阅读 CppCon 2014
CppCon 2014: Walter E. Brown "Modern Template Metaprogramming: A Compendium, Part I
1.2 为什么能带来这样收益,第一性原理或者核心实现原理 是什么?
std::is_same_v 是 C++17 引入的编译期类型比较工具,
用于判断两个类型是否严格相同。
if T and U name the same type (taking into account const/volatile qualifications),
provides the member constant value equal to true. Otherwise value is false.
● 具备内部实现先不考虑
● is_same_v 是 is_same::value 的简写:
template
inline constexpr bool is_same_v = is_same::value;
其底层基于模板元编程中的偏特化机制实现:
与 typeid 的对比
特性        std::is_same_v        typeid
比较阶段        编译期        运行时
多态类型        静态类型        动态类型(可能派发到派生类)
性能影响        零开销        RTTI 开销
适用场景        模板元编程、静态检查        运行时类型识别、调试
typeid 运算符
// expre_typeid_Operator.cpp
// compile with: /GR /EHsc
include

include

class Base {
public:
virtual void vvfunc() {}
};
class Derived : public Base {};
using namespace std;

//typeid 运算符允许在运行时确定对象的类型。
int main() {
Derived* pd = new Derived;
Base* pb = pd;
cout
您需要登录后才可以回帖 登录 | 立即注册