mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
see #2: move non-type template param auto to ch02
This commit is contained in:
@@ -19,5 +19,5 @@ template<bool T> class MagicType {
|
||||
int main() {
|
||||
// the >> in template
|
||||
std::vector<std::vector<int>> matrix;
|
||||
std::vector<MagicType<(1>2)>> magic;
|
||||
std::vector<MagicType<(1>2)>> magic; // legal, but not recommended
|
||||
}
|
||||
18
code/2/2.14.default.template.param.cpp
Normal file
18
code/2/2.14.default.template.param.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// 2.4.default.template.param.cpp
|
||||
// chapter 2 language usability
|
||||
// modern cpp tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
template<typename T = int, typename U = int>
|
||||
auto add(T x, U y) -> decltype(x+y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << add(1, 2) << std::endl;
|
||||
}
|
||||
46
code/2/2.15.variadic.template.param.cpp
Normal file
46
code/2/2.15.variadic.template.param.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// 2.15.variadic.template.param.cpp
|
||||
// chapter 2 language usability
|
||||
// modern cpp tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
// sizeof...
|
||||
template<typename... Ts>
|
||||
void magic(Ts... args) {
|
||||
std::cout << sizeof...(args) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
// 1 recursive parameter unpack
|
||||
template<typename T0>
|
||||
void printf1(T0 value) {
|
||||
std::cout << value << std::endl;
|
||||
}
|
||||
template<typename T, typename... Ts>
|
||||
void printf1(T value, Ts... args) {
|
||||
std::cout << value << std::endl;
|
||||
printf1(args...);
|
||||
}
|
||||
|
||||
// 2 variadic template parameter unfold
|
||||
template<typename T0, typename... T>
|
||||
void printf2(T0 t0, T... t) {
|
||||
std::cout << t0 << std::endl;
|
||||
if constexpr (sizeof...(t) > 0) printf2(t...);
|
||||
}
|
||||
|
||||
int main() {
|
||||
magic();
|
||||
magic(1);
|
||||
magic(1,"");
|
||||
|
||||
printf1(1, 2, "123", 1.1);
|
||||
printf2(1, 2.3, "abc");
|
||||
return 0;
|
||||
}
|
||||
@@ -1,3 +1,11 @@
|
||||
//
|
||||
// 2.16.fold.expression.cpp
|
||||
// chapter 2 language usability
|
||||
// modern cpp tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
template<typename ... T>
|
||||
auto sum(T ... t) {
|
||||
18
code/2/2.18.non.type.template.auto.cpp
Normal file
18
code/2/2.18.non.type.template.auto.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// 2.18.non.type.template.auto.cpp
|
||||
// chapter 2 language usability
|
||||
// modern cpp tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
template <auto value> void foo() {
|
||||
std::cout << value << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
foo<10>(); // value is deduced as type int
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
//
|
||||
// 2.6.cpp
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
// 模板增强
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
template class std::vector<bool>; // 强行实例化
|
||||
extern template class std::vector<double>; // 不在该编译文件中实例化模板
|
||||
|
||||
|
||||
template< typename T, typename U, int value>
|
||||
class SuckType {
|
||||
public:
|
||||
T a;
|
||||
U b;
|
||||
SuckType():a(value),b(value){}
|
||||
};
|
||||
// template< typename T>
|
||||
// typedef SuckType<std::vector<int>, T, 1> NewType; // 不合法
|
||||
|
||||
template <typename T>
|
||||
using NewType = SuckType<int, T, 1>; // 合法
|
||||
|
||||
// 默认模板类型
|
||||
template<typename T = int, typename U = int>
|
||||
auto add(T x, U y) -> decltype(x+y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
// sizeof...
|
||||
template<typename... Args>
|
||||
void magic(Args... args) {
|
||||
std::cout << sizeof...(args) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
// 1. 递归解参数包
|
||||
template<typename T>
|
||||
void printf1(T value) {
|
||||
std::cout << value << std::endl;
|
||||
}
|
||||
template<typename T, typename... Args>
|
||||
void printf1(T value, Args... args) {
|
||||
std::cout << value << std::endl;
|
||||
printf1(args...);
|
||||
}
|
||||
// 2.初始化列表展开解参数包
|
||||
template<typename T, typename... Args>
|
||||
auto printf2(T value, Args... args) {
|
||||
std::cout << value << std::endl;
|
||||
return std::initializer_list<T>{([&] {
|
||||
std::cout << args << std::endl;
|
||||
}(), value)...};
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
std::vector<std::vector<int>> wow; // 注意尖括号
|
||||
|
||||
NewType<int> t;
|
||||
|
||||
magic();
|
||||
magic(1);
|
||||
magic(1,"");
|
||||
|
||||
printf1(1, 2.3, "abc");
|
||||
printf2(1, 2.3, "abc");
|
||||
return 0;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user