diff --git a/note/learn/learn.md b/note/learn/learn.md index 36a1250..50ed327 100644 --- a/note/learn/learn.md +++ b/note/learn/learn.md @@ -1,6 +1,6 @@ ## lib - [[devops]] - - [[Docker]] + - [[note/learn/lib/devops/Docker]] - [[LNMP 镜像]] - [[xmake]] - [[git]] diff --git a/src/c/c++/object initial.md b/src/c/c++/object initial.md new file mode 100644 index 0000000..53674c7 --- /dev/null +++ b/src/c/c++/object initial.md @@ -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 x2{ 1,2,3,4,5 }; +- 聚合类型由编辑器提供 列表初始化构造函数 +- 支持指定初始化 +- 列表初始化编译更严格,不允许隐式转换 + +```c++ +#include +#include + +struct C { + C(std::initializer_list 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 }; +``` + + + +# 聚合类型 + +- 支持列表初始化 +- 不能有构造函数 +- 不能有虚函数 diff --git a/src/c/c++/template.md b/src/c/c++/template.md new file mode 100644 index 0000000..2cec369 --- /dev/null +++ b/src/c/c++/template.md @@ -0,0 +1,234 @@ +--- +aliases: +tags: +creation date: 2024-03-29 13:31 +modification date: 星期五 29日 三月 2024 13:31:31 +--- +# 模板 + +## 可变参数模板 + +- 函数模板 + +```c++ +//模板参数包,模板类型、数量可变 +template +//函数参数包,参数类型、数量可变 +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 +struct Tuple : Tuple { + Ty1 val; + using Base = Tuple; + Tuple() {} + Tuple(Ty1 v, Ty2... args) : val(v), Base(args...) {} +}; +template +struct Tuple_element { + using Type = typename Tuple_element::Type; +}; +template +struct Tuple_element<0, _Tuple> { + using Type = _Tuple; +}; +template +constexpr auto& Get(_Tuple& t) { + using Type = typename Tuple_element < idx, _Tuple>::Type; + return static_cast(t).val; +} + +Tuplex(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 +concept TemplateConcept = std::is_integral_v; + +//concept 用于模板声明里 +template +struct X {}; +``` + + + +- requires + +```c++ +//requires 直接使用 或 通过概念使用 + +//requires 初等布尔表达式 或 带括号的 任意表达式 +template +concept Check1 = requires std::is_integral_v || ([]{ return false; }()); + +//requires 类型表达式 表达能力弱 +template +concept Check2 = requires { + T() + T(); +}; + +//requires 类型表达式 表达能力强 +template +concept Check3 = requires(T a, T b){ + a + b; +}; + +//requires 特殊检测 +template struct S; +template using Ref = T&; +template concept Check4 = requires { + typename T::inner; // 要求嵌套类型 + typename S; // 要求类模板特化 + typename Ref; // 要求别名模板特化 +}; + +//requires 复合要求 异常、返回类型 +template +concept Check = requires(T a, T b) { + {a.clear()} noexcept; + {a + b} noexcept -> std::same_as; +}; +//requires 嵌套要求 +template +concept Check = requires(T a, T b) { + requires std::same_as; +}; +``` + +## 递归模板 + +- 斐波那契数列 + +```c++ +template +struct Fac { + const static int value = N * Fac::value; +}; + +template<> +struct Fac<0> { + const static int value = 1; +}; + +``` + +- 最大公约数 + +```c++ +template +struct Gcd { + const static int value = Gcd::value; +}; +template +struct Gcd { + const static int value = a; +}; + +``` + +## 条件模板 + +- 选择函数 + +```cpp +template +struct IF {}; +template +struct IF { + using ret = True; +}; +template +struct IF { + using ret = False; +}; + +template +struct Choose { + const static int value = + IF< + N % 2 == 1, + Fac, + Fib + >::ret::value; +}; +``` + +- 判断质数 + +```c++ +template +struct Int { + const static int value = N; +}; +template +struct is_Prime { + const static int value = + IF< + (i * i> N), + Int<1>, + IF< + ( N% i == 0), + Int<0>, + is_Prime + >::ret + > ::ret::value; +}; +``` + +# 特殊模板 + +## std::tuple + +- 依据模板参数构造struct +- 通过继承间接构造 || 直接构造 + +## std::any + +- 依据模板生成类的内部包装类,继承内部基类 +- 持有基类指针 + +## std::variant + +- 构造类似union的自定义对象 +- 对象自动析构 + +## std::function + +- 函数也是对象,把它看成特殊对象 +- 依据模板生成函数的内部包装类,继承内部基类 +- 持有基类指针 + +## std::any_ref || std::function_ref + +- 使用void* 指向目标对象 +- 生成类型包装函数,传入指针和参数。会转变成实际类型处理 +- 函数指针,指向包装函数 + diff --git a/src/c/c++/type.md b/src/c/c++/type.md new file mode 100644 index 0000000..aaace77 --- /dev/null +++ b/src/c/c++/type.md @@ -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 : + public FieldTraits , + NamedValue +{ + AList attrs = {}; +} +``` + + + +- FieldList + - ElemList + - std::tuple:: elems + - - size_t::size + +```c++ +struct FieldList : Element{ + +} +``` + +## UDRefl + +- Type + - +- ObjectView + - Type::type + - void*::ptr +- SharedObject + + + +```c++ +#include +#include + +template +struct MyClass { + // 如果 T 类型拥有 InnerType 内部类型,则使用它 + using InnerType = void; +}; + +template +struct MyClass> { + using InnerType = typename T::InnerType; +}; + +// 定义一个不具有 InnerType 内部类型的类 +struct WithoutInnerType {}; +struct WithInnerType { + using InnerType = int; +}; +int main() { + // 使用 MyClass 模板 struct,将内部类型导出 + using Type2 = MyClass::InnerType; // InnerType 是 void 类型 + using Type1 = MyClass::InnerType; // InnerType 是 void 类型 + // 输出内部类型 + std::cout << "Type2: " << typeid(Type2).name() << std::endl; + std::cout << "Type1: " << typeid(Type1).name() << std::endl; + return 0; +} + +``` diff --git a/src/c/c++/value type.md b/src/c/c++/value type.md index eb90f0b..da74f21 100644 --- a/src/c/c++/value type.md +++ b/src/c/c++/value type.md @@ -85,7 +85,7 @@ modification date: 星期五 2日 二月 2024 13:17:0 ## 移动构造 -``` +```c++ #include #include #include @@ -149,3 +149,25 @@ namespace zstd { } ``` +```c++ +void foo(int & x) { + cout << "int &" << endl; +} +void foo(int&& x) { + cout << "int&&" << endl; +} +template +void check(T && x) { + if (is_same_v)cout << "int ->"; + if (is_same_v)cout << "int& ->"; + if (is_same_v)cout << "int&& ->"; + foo(forward(x)); +} +int main() +{ + int x; + check(x); + check(move(x)); +} +``` + diff --git a/src/z/zengine/渲染.md b/src/z/zengine/渲染.md new file mode 100644 index 0000000..f760b3b --- /dev/null +++ b/src/z/zengine/渲染.md @@ -0,0 +1,23 @@ +--- +aliases: +tags: +creation date: 2024-04-30 10:16 +modification date: 星期二 30日 四月 2024 10:16:59 +--- +# 渲染体系 + +## 概念 + +### 着色器 + +- 顶点输入布局 +- 着色器参数 + +### 顶点工厂 + +- 顶点布局 + +### 材质 + +- 着色器参数 +