revision #2: 检阅部分模板相关内容

This commit is contained in:
Changkun Ou
2018-04-09 14:48:19 +02:00
parent 02a1105512
commit cc294b211c
3 changed files with 89 additions and 18 deletions

View File

@@ -0,0 +1,23 @@
//
// 2.12.external.template.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//
#include <iostream>
#include <vector>
template class std::vector<bool>; // forcely instantiation
extern template class std::vector<double>; // external template for avoiding instantiation in this file
template<bool T> class MagicType {
bool magic = T;
};
int main() {
// the >> in template
std::vector<std::vector<int>> matrix;
std::vector<MagicType<(1>2)>> magic;
}

View File

@@ -0,0 +1,32 @@
//
// 2.13.alias.template.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//
#include <iostream>
#include <vector>
#include <string>
template<typename T, typename U>
class MagicType {
public:
T dark;
U magic;
};
// illegal
// template<typename T>
// typedef MagicType<std::vector<T>, std::string> FakeDarkMagic;
typedef int (*process)(void *);
using NewProcess = int(*)(void *);
template<typename T>
using TrueDarkMagic = MagicType<std::vector<T>, std::string>;
int main() {
// FakeDarkMagic<bool> me;
TrueDarkMagic<bool> you;
}