update c++ refl

This commit is contained in:
ouczbs 2024-04-30 17:33:36 +08:00
parent 1379d575ac
commit d63af3eac3
6 changed files with 516 additions and 2 deletions

View File

@ -1,6 +1,6 @@
## lib ## lib
- [[devops]] - [[devops]]
- [[Docker]] - [[note/learn/lib/devops/Docker]]
- [[LNMP 镜像]] - [[LNMP 镜像]]
- [[xmake]] - [[xmake]]
- [[git]] - [[git]]

View File

@ -0,0 +1,67 @@
---
aliases:
tags:
creation date: 2024-03-29 14:51
modification date: 星期五 29日 三月 2024 14:51:38
---
# 对象初始化
- 初始化顺序为对象定义顺序
- 所以对象有依赖时,要确保定义顺序合法
- 初始化时,相当于调用对应的构造函数
## 默认初始化
- int a = 1;
- int b {1};
- int c(2);//非法
## 位域初始化
- int a : 8 = 1
- int b : 4 {4};
- int (c : 4) = 3;
## 列表初始化
- std::vector<int> x2{ 1,2,3,4,5 };
- 聚合类型由编辑器提供 列表初始化构造函数
- 支持指定初始化
- 列表初始化编译更严格,不允许隐式转换
```c++
#include <iostream>
#include <string>
struct C {
C(std::initializer_list<std::string> a)
{
for (const std::string* item = a.begin(); item != a.end(); ++item) {
std::cout << *item << " ";
}
std::cout << std::endl;
}
};
int main()
{
C c{ "hello", "c++", "world" };
}
```
```c++
struct Point {
int x;
int y;
};
Point p{ .x = 4, .y = 2 };
```
# 聚合类型
- 支持列表初始化
- 不能有构造函数
- 不能有虚函数

234
src/c/c++/template.md Normal file
View File

@ -0,0 +1,234 @@
---
aliases:
tags:
creation date: 2024-03-29 13:31
modification date: 星期五 29日 三月 2024 13:31:31
---
# 模板
## 可变参数模板
- 函数模板
```c++
//模板参数包,模板类型、数量可变
template <typename T, typename ...Args>
//函数参数包,参数类型、数量可变
void printMsg(const T& t, const Args&... args) {
std::cout << t << ", ";
//参数包 解包,args...是包展开args 是模式,似乎包展开位于()内
//这也是一条合法的逗号表达式语句(args,...);
printMsg(args...);
}
// 调用
printMsg(1, 0.3f, "success");
```
- tuple
```c++
template<>struct Tuple<> {};
template<typename Ty1, typename ...Ty2>
struct Tuple<Ty1, Ty2...> : Tuple<Ty2...> {
Ty1 val;
using Base = Tuple<Ty2...>;
Tuple() {}
Tuple(Ty1 v, Ty2... args) : val(v), Base(args...) {}
};
template<int idx, typename _Tuple>
struct Tuple_element {
using Type = typename Tuple_element<idx - 1, typename _Tuple::Base>::Type;
};
template<typename _Tuple >
struct Tuple_element<0, _Tuple> {
using Type = _Tuple;
};
template<int idx, typename _Tuple>
constexpr auto& Get(_Tuple& t) {
using Type = typename Tuple_element < idx, _Tuple>::Type;
return static_cast<Type&>(t).val;
}
Tuple<int, int, char, string>x(114, 514, 'a', "soul");
Get<0>(x) = 1919;
Get<1>(x) = 810;
cout << Get<0>(x) << Get<1>(x) << endl;
cout << Get<2>(x) << Get<3>(x) << endl
```
## 模板约束
模板约束必须要在编译期间就完成计算
- concept
```C++
//常量布尔,缺少实际意义
concept ConstBoolConcept = true;
//模板布尔,用于约束类型
template <class C>
concept TemplateConcept = std::is_integral_v<C>;
//concept 用于模板声明里
template <TemplateConcept T>
struct X {};
```
- requires
```c++
//requires 直接使用 或 通过概念使用
//requires 初等布尔表达式 或 带括号的 任意表达式
template <class T>
concept Check1 = requires std::is_integral_v<T> || ([]{ return false; }());
//requires 类型表达式 表达能力弱
template <class T>
concept Check2 = requires {
T() + T();
};
//requires 类型表达式 表达能力强
template <class T>
concept Check3 = requires(T a, T b){
a + b;
};
//requires 特殊检测
template<typename T, typename T::type = 0> struct S;
template<typename T> using Ref = T&;
template<typename T> concept Check4 = requires {
typename T::inner; // 要求嵌套类型
typename S<T>; // 要求类模板特化
typename Ref<T>; // 要求别名模板特化
};
//requires 复合要求 异常、返回类型
template <class T>
concept Check = requires(T a, T b) {
{a.clear()} noexcept;
{a + b} noexcept -> std::same_as<int>;
};
//requires 嵌套要求
template <class T>
concept Check = requires(T a, T b) {
requires std::same_as<decltype((a + b)), int>;
};
```
## 递归模板
- 斐波那契数列
```c++
template<int N>
struct Fac {
const static int value = N * Fac<N - 1>::value;
};
template<>
struct Fac<0> {
const static int value = 1;
};
```
- 最大公约数
```c++
template<int a, int b>
struct Gcd {
const static int value = Gcd<b, a% b>::value;
};
template<int a>
struct Gcd<a, 0> {
const static int value = a;
};
```
## 条件模板
- 选择函数
```cpp
template <bool flag, typename True, typename False>
struct IF {};
template<typename True, typename False>
struct IF<true, True, False> {
using ret = True;
};
template<typename True, typename False>
struct IF<false, True, False> {
using ret = False;
};
template<int N>
struct Choose {
const static int value =
IF<
N % 2 == 1,
Fac<N>,
Fib<N>
>::ret::value;
};
```
- 判断质数
```c++
template<int N>
struct Int {
const static int value = N;
};
template<int i,int N>
struct is_Prime {
const static int value =
IF<
(i * i> N),
Int<1>,
IF<
( N% i == 0),
Int<0>,
is_Prime<i + 1,N>
>::ret
> ::ret::value;
};
```
# 特殊模板
## std::tuple
- 依据模板参数构造struct
- 通过继承间接构造 || 直接构造
## std::any
- 依据模板生成类的内部包装类,继承内部基类
- 持有基类指针
## std::variant
- 构造类似union的自定义对象
- 对象自动析构
## std::function
- 函数也是对象,把它看成特殊对象
- 依据模板生成函数的内部包装类,继承内部基类
- 持有基类指针
## std::any_ref || std::function_ref
- 使用void* 指向目标对象
- 生成类型包装函数,传入指针和参数。会转变成实际类型处理
- 函数指针,指向包装函数

168
src/c/c++/type.md Normal file
View File

@ -0,0 +1,168 @@
---
aliases:
tags:
creation date: 2024-03-30 13:45
modification date: 星期六 30日 三月 2024 13:45:26
---
# 反射
## 属性
- 静态遍历属性
- 动态遍历属性
- + 模板 => 静态属性遍历
- 获得属性
## 方法
## 常量表达式
- 可以直接获得常量结果,省掉中间计算
# 类型
- 存在与编译期间
- 类型主要用于确定链接的类型函数与变量地址
- 虚函数通过类型对应的虚表,可以实现运行时多态
## 类型擦除
如何用一个基础类型,储存多个特殊类型?特殊类型被擦除了特殊性,如何从基础类型还原到特殊类型?
- void *
- 有来无回,不安全
- 只适用于单类型,强制转换
- 父子类
- 功能强大,支持多态,即可满足大多数需求
- 特殊需求,父类转子类,亦可安全转化
- 限制大,需要类继承、对象新增虚表地址,浪费空间
- union + type_index
- 需手动定义结构体转化时需要case 语句
- 需管理子对象的生命周期,构造与析构
- 能用模板简化吗?
- std::variant && std::visit
- 函数数组 => 依据保护函数重载的类 || 泛型Lambda 自动推导 + constexpr 生成不同代码
- 依据variant索引ID直接跳转到函数
- 使用繁琐 ,但比 union 好用
- std::any
- 定义一个Any类内部为所有类型生成包装类模板类包装类继承基类
- any 类持有基类指针
- 需要new 对象
## 类型复原
能借助基础类型,调用到特殊类型的接口
- 虚表
- 父子类
- 包装父子类
- 模板
- 为每一种类型,生成对应的代码
- 代码里存储的接口函数不同
## 函数调用
类型转换
| FROM | ARGS | TO |
| ---- | ------------ | ------------ |
| T | T=>T* | T*=>T |
| T* | T * => T * | T* => T* |
| T& | T& => T* | T* => T& |
| T&& | T&&=>T* | T* => T&& |
| T** | T * *=>T * * | T * *=>T * * |
# 反射库
运行时存储、获取额外的类型信息
: cannot cast from type '(lambda' to pointer type
## USRefl
- Field
- FieldTrait
- - object_type = Obj
- - value_type = Value
- - bool::is_static
- - bool::is_func
- NamedValue
- - TName = Name
- - string_view::name
- T::value
- AList::attrs
```c++
struct Vec1{
float x;
float y;
}
struct Vec{
Vec1 v1;
float y;
}
struct Field<Name="vec1",T=Vec1,AList = {}> :
public FieldTraits<Object = Vec, T = Vec1> ,
NamedValue<Name="vec1",T=Vec1>
{
AList attrs = {};
}
```
- FieldList
- ElemList
- std::tuple<Field...>:: elems
- - size_t::size
```c++
struct FieldList<Field1, Field2, Field3...> : Element{
}
```
## UDRefl
- Type
-
- ObjectView
- Type::type
- void*::ptr
- SharedObject
```c++
#include <iostream>
#include <type_traits>
template<typename T, typename = void>
struct MyClass {
// 如果 T 类型拥有 InnerType 内部类型,则使用它
using InnerType = void;
};
template<typename T>
struct MyClass<T, std::void_t<typename T::InnerType>> {
using InnerType = typename T::InnerType;
};
// 定义一个不具有 InnerType 内部类型的类
struct WithoutInnerType {};
struct WithInnerType {
using InnerType = int;
};
int main() {
// 使用 MyClass 模板 struct将内部类型导出
using Type2 = MyClass<WithoutInnerType>::InnerType; // InnerType 是 void 类型
using Type1 = MyClass<WithInnerType>::InnerType; // InnerType 是 void 类型
// 输出内部类型
std::cout << "Type2: " << typeid(Type2).name() << std::endl;
std::cout << "Type1: " << typeid(Type1).name() << std::endl;
return 0;
}
```

View File

@ -85,7 +85,7 @@ modification date: 星期五 2日 二月 2024 13:17:0
## 移动构造 ## 移动构造
``` ```c++
#include <condition_variable> #include <condition_variable>
#include <semaphore> #include <semaphore>
#include <iostream> #include <iostream>
@ -149,3 +149,25 @@ namespace zstd {
} }
``` ```
```c++
void foo(int & x) {
cout << "int &" << endl;
}
void foo(int&& x) {
cout << "int&&" << endl;
}
template<typename T>
void check(T && x) {
if (is_same_v<T, int>)cout << "int ->";
if (is_same_v<T, int&>)cout << "int& ->";
if (is_same_v<T, int&&>)cout << "int&& ->";
foo(forward<T>(x));
}
int main()
{
int x;
check(x);
check(move(x));
}
```

23
src/z/zengine/渲染.md Normal file
View File

@ -0,0 +1,23 @@
---
aliases:
tags:
creation date: 2024-04-30 10:16
modification date: 星期二 30日 四月 2024 10:16:59
---
# 渲染体系
## 概念
### 着色器
- 顶点输入布局
- 着色器参数
### 顶点工厂
- 顶点布局
### 材质
- 着色器参数