Skip to content

Commit

Permalink
git@github.com:PredyDaddy/My_CPP_Note.git
Browse files Browse the repository at this point in the history
  • Loading branch information
PredyDaddy committed Feb 14, 2023
1 parent ff5f99b commit 9707236
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
38 changes: 38 additions & 0 deletions 13.template_demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,41 @@ int main() {
```
9. 可以解释一下模版类的实现原理?
- 模板类是一种将数据类型抽象化和参数化的编程技术,使得同一套类定义可以适用于不同的数据类型和参数。模板类定义了一种通用的类模板,实际上并不是一个真正的类,而是一个用于生成类的蓝图。在使用时,编译器会根据特定的模板参数实例化出对应的类。
10. 使用模板类限制模版类型的属性和方法 concept(c++ 20的新特性)
```cpp
template <typename T>
struct HasValue {
template <typename U>
static std::true_type test(decltype(&U::value));
template <typename U>
static std::false_type test(...);
static constexpr bool value = decltype(test<T>(nullptr))::value;
};
template <typename T>
typename std::enable_if<HasValue<T>::value>::type
printValue(T arg) {
std::cout << "Value: " << arg.value << std::endl;
}
struct MyStruct {
int value = 42;
};
struct MyOtherStruct {
float otherValue = 3.14f;
int value = 43;
};
int main() {
MyStruct s;
MyOtherStruct os;
printValue(s); // 输出:Value: 42
// 下面这行代码会在编译时出现错误,因为MyOtherStruct类型不满足HasValue的限定条件
printValue(os);
}
```
Binary file modified 13.template_demo/class_template
Binary file not shown.
42 changes: 41 additions & 1 deletion 13.template_demo/class_template.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <type_traits>
using namespace std;

// 类模版的定义
Expand Down Expand Up @@ -207,7 +208,7 @@ int main()
#endif

// 子类父类都是类模版
#if 1
#if 0
template <typename T1, typename T2>
class A
{
Expand Down Expand Up @@ -239,4 +240,43 @@ int main()
C<int> c;
return 0;
}
#endif

//使用模板类限制模版类型的属性和方法
#if 1
template <typename T>
struct HasValue {
template <typename U>
static std::true_type test(decltype(&U::value));

template <typename U>
static std::false_type test(...);

static constexpr bool value = decltype(test<T>(nullptr))::value;
};

template <typename T>
typename std::enable_if<HasValue<T>::value>::type
printValue(T arg) {
std::cout << "Value: " << arg.value << std::endl;
}

struct MyStruct {
int value = 42;
};

struct MyOtherStruct {
float otherValue = 3.14f;
int value = 43;
};

int main() {
MyStruct s;
MyOtherStruct os;

printValue(s); // 输出:Value: 42

// 下面这行代码会在编译时出现错误,因为MyOtherStruct类型不满足HasValue的限定条件
printValue(os);
}
#endif
53 changes: 28 additions & 25 deletions 13.template_demo/code.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
```cpp
// 子类父类都是类模版
template <typename T1, typename T2>
class A
{
// 类的默认访问修饰词是private, 所以这里的x, y是私有属性
// 如果向指定为公有的,需要修改饰词为public
T1 x;
T2 y;
template <typename T>
struct HasValue {
template <typename U>
static std::true_type test(decltype(&U::value));

template <typename U>
static std::false_type test(...);

static constexpr bool value = decltype(test<T>(nullptr))::value;
};

template <typename T1, typename T2>
class B : public A<T2, T1>
{
T1 x1;
T2 x2;
template <typename T>
typename std::enable_if<HasValue<T>::value>::type
printValue(T arg) {
std::cout << "Value: " << arg.value << std::endl;
}

struct MyStruct {
int value = 42;
};

template<typename T>
class C : public B<T, T>
{
T x3;
struct MyOtherStruct {
float otherValue = 3.14f;
int value = 43;
};

int main()
{
// B中的T1和A中的T1 泛指int, B中的T2,A中的T1 泛指float
B<int, float> b;
int main() {
MyStruct s;
MyOtherStruct os;

printValue(s); // 输出:Value: 42

// 这样A,B,C中全部泛指类型都是int
C<int> c;
return 0;
// 下面这行代码会在编译时出现错误,因为MyOtherStruct类型不满足HasValue的限定条件
printValue(os);
}
```
```

0 comments on commit 9707236

Please sign in to comment.