mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
revision #1: 更新第二章中已维护的代码
This commit is contained in:
49
code/2/todo/2.6.autox.cpp
Normal file
49
code/2/todo/2.6.autox.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// 2.3.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
// auto/decltype/尾返回类型/返回类型推导
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// 传统 C++
|
||||
template <typename R, typename T, typename U>
|
||||
R add1(T x, U y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
// 尾返回类型
|
||||
template <typename T, typename U>
|
||||
auto add2(T x, U y) -> decltype(x+y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
// C++14 返回类型推导
|
||||
template <typename T, typename U>
|
||||
auto add3(T x, U y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto i = 5;
|
||||
|
||||
int arr[10] = {0};
|
||||
auto auto_arr = arr; // 正确,对整个类型进行推导
|
||||
// auto auto_arr2[10] = arr; // 错误, 无法推导数组元素类型
|
||||
|
||||
auto x = 1;
|
||||
auto y = 2;
|
||||
decltype(x+y) z1;
|
||||
//auto z2; // 错误, 无法推导
|
||||
|
||||
|
||||
std::cout << add1<int, int, int>(1,1) << std::endl;
|
||||
std::cout << add1<int, int>(1,1) << std::endl;
|
||||
std::cout << add1<int, int>(1,1) << std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
65
code/2/todo/2.7.cpp
Normal file
65
code/2/todo/2.7.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// 2.7.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
// 面向对象增强
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
class Base {
|
||||
public:
|
||||
std::string str;
|
||||
int value;
|
||||
Base() = delete;
|
||||
Base(std::string s) {
|
||||
str = s;
|
||||
}
|
||||
|
||||
// 委托构造
|
||||
Base(std::string s, int v) : Base(s) {
|
||||
value = v;
|
||||
}
|
||||
|
||||
// 终止重载
|
||||
virtual void foo() final {
|
||||
return;
|
||||
}
|
||||
virtual void foo(int v) {
|
||||
value = v;
|
||||
}
|
||||
};
|
||||
class Subclass final : public Base {
|
||||
public:
|
||||
double floating;
|
||||
Subclass() = delete;
|
||||
|
||||
// 继承构造
|
||||
Subclass(double f, int v, std::string s) : Base(s, v) {
|
||||
floating = f;
|
||||
}
|
||||
|
||||
// 显式重载
|
||||
virtual void foo(int v) override {
|
||||
std::cout << v << std::endl;
|
||||
value = v;
|
||||
}
|
||||
}; // 合法 final
|
||||
|
||||
// class Subclass2 : Subclass {
|
||||
// }; // 非法, Subclass 已 final
|
||||
// class Subclass3 : Base {
|
||||
// void foo(); // 非法, foo 已 final
|
||||
// }
|
||||
|
||||
int main() {
|
||||
// Subclass oops; // 非法, 默认构造已删除
|
||||
Subclass s(1.2, 3, "abc");
|
||||
|
||||
s.foo(1);
|
||||
|
||||
std::cout << s.floating << std::endl;
|
||||
std::cout << s.value << std::endl;
|
||||
std::cout << s.str << std::endl;
|
||||
}
|
||||
51
code/2/todo/2.8.cpp
Normal file
51
code/2/todo/2.8.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// 2.8.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
// 强类型枚举
|
||||
|
||||
#include <iostream>
|
||||
template<typename T>
|
||||
std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
|
||||
{
|
||||
return stream << static_cast<typename std::underlying_type<T>::type>(e);
|
||||
}
|
||||
|
||||
// 如果两个都定义为 value1 和 value2,将引发重定义错误
|
||||
enum Left {
|
||||
left_value1 = 1,
|
||||
left_value2
|
||||
};
|
||||
enum Right {
|
||||
right_value1 = 1,
|
||||
right_value2
|
||||
};
|
||||
|
||||
enum class new_enum : unsigned int{
|
||||
value1,
|
||||
value2,
|
||||
value3 = 100,
|
||||
value4 = 100
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
if (Left::left_value1 == Right::right_value2) {
|
||||
std::cout << "Left::value1 == Right::value2" << std::endl;
|
||||
}
|
||||
|
||||
// 引发编译错误
|
||||
// if(new_enum::value1 == 1) {
|
||||
// std::cout << "true!" << std::endl;
|
||||
// }
|
||||
if (new_enum::value3 == new_enum::value4) {
|
||||
std::cout << "new_enum::value3 == new_enum::value4" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << new_enum::value3 << std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
code/2/todo/2.xxx.cpp
Normal file
21
code/2/todo/2.xxx.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
|
||||
template<typename T0>
|
||||
void printf(T0 value) {
|
||||
std::cout << value << std::endl;
|
||||
}
|
||||
template<typename T, typename... Args>
|
||||
void printf(T value, Args... args) {
|
||||
std::cout << value << std::endl;
|
||||
printf(args...);
|
||||
}
|
||||
|
||||
template<typename T0, typename... T>
|
||||
void printf_short(T0 t0, T... t) {
|
||||
std::cout << t0 << std::endl;
|
||||
if constexpr (sizeof...(t) > 0) printf(t...);
|
||||
}
|
||||
int main() {
|
||||
printf_short(1, 2, "123", 1.1);
|
||||
return 0;
|
||||
}
|
||||
8
code/2/todo/fold.expression.cpp
Normal file
8
code/2/todo/fold.expression.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
template<typename ... T>
|
||||
auto sum(T ... t) {
|
||||
return (t + ...);
|
||||
}
|
||||
int main() {
|
||||
std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user