#include <array>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
template <typename T>
inline T const& MaxVal(T const& a, T const& b) {
return a < b ? b : a;
}
template <class T>
class Stack {
private:
std::vector<T> elems;
public:
void push(T const& elem);
void pop();
T top() const;
bool empty() const { return elems.empty(); }
};
template <class T>
void Stack<T>::push(T const& elem) {
elems.push_back(elem);
}
template <class T>
void Stack<T>::pop() {
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back();
}
template <class T>
T Stack<T>::top() const {
if (elems.empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return elems.back();
}
template <typename K, typename V, std::size_t N>
struct FixedTable {
std::array<std::pair<K, V>, N> entries;
std::size_t capacity() const { return N; }
};
template <typename T>
struct TypeTag {
static std::string label() { return "generic"; }
};
template <>
struct TypeTag<bool> {
static std::string label() { return "full_bool"; }
};
template <typename T>
struct TypeTag<T*> {
static std::string label() { return "partial_ptr"; }
};
int main() {
// 1. Function template type deduction across int, double, std::string
int max_i = MaxVal(39, 20);
double max_d = MaxVal(13.5, 20.7);
std::string max_s = MaxVal(std::string("Hello"), std::string("World"));
std::ostringstream oss;
oss << max_i << "|" << std::fixed << std::setprecision(1) << max_d << "|" << max_s;
std::string act1 = oss.str();
const std::string exp1 = "39|20.7|World";
bool ok1 = (act1 == exp1);
// 2. Class template Stack<T> with int, string, and empty pop exception
Stack<int> intStack;
Stack<std::string> strStack;
intStack.push(7);
strStack.push("hello");
int top_i = intStack.top();
std::string top_s = strStack.top();
strStack.pop();
std::string err_msg;
try {
strStack.pop();
} catch (const std::out_of_range& ex) {
err_msg = ex.what();
}
std::string act2 = std::to_string(top_i) + "|" + top_s + "|" + err_msg;
const std::string exp2 = "7|hello|Stack<>::pop(): empty stack";
bool ok2 = (act2 == exp2);
// 3. Multiple type parameters & non-type template parameter
FixedTable<std::string, int, 2> tbl{{std::make_pair("alpha", 1), std::make_pair("beta", 2)}};
std::string act3 = std::to_string(tbl.capacity()) + ":" + tbl.entries[0].first + "=" +
std::to_string(tbl.entries[0].second) + "," + tbl.entries[1].first + "=" +
std::to_string(tbl.entries[1].second);
const std::string exp3 = "2:alpha=1,beta=2";
bool ok3 = (act3 == exp3);
// 4. Primary template, full specialization, and partial pointer specialization
std::string act4 = TypeTag<int>::label() + "|" + TypeTag<bool>::label() + "|" + TypeTag<int*>::label();
const std::string exp4 = "generic|full_bool|partial_ptr";
bool ok4 = (act4 == exp4);
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"function_template_deduction\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"class_template_stack_and_exception\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"multi_type_and_nontype_template\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"template_full_and_partial_specialization\", \"expected\": \"" << exp4
<< "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
<< " ]\n"
<< "}\n";
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-templates-lab/explore-cpp-templates.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-templates.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2399.zip