关于 C++中 type_name 关键字的理解与应用
今天在 Github 上有看到一个有趣的项目:dbg-macro。见名知意:其功能类似于一个 Debug 宏,但比较有趣的地方在于,其能够输出变成语句,以及相应的结果,而不需要我们通过printf等形式输出具体信息。 以下是一个具体的例子:
#include <vector> #include <dbg.h> // You can use "dbg(..)" in expressions: int factorial(int n) { if (dbg(n <= 1)) { return dbg(1); } else { return dbg(n * factorial(n - 1)); } } int main() { std::string message = "hello"; dbg(message); // [example.cpp:15 (main)] message = "hello" (std::string) const int a = 2; const int b = dbg(3 * a) + 1; // [example.cpp:18 (main)] 3 * a = 6 (int) std::vector<int> numbers{b, 13, 42}; dbg(numbers); // [example.cpp:21 (main)] numbers = {7, 13, 42} (size: 3) (std::vector<int>) dbg("this line is executed"); // [example.cpp:23 (main)] this line is executed factorial(4); return 0; } 本人在查看源码的过程中,对于以下实现中的dbg_macro::type_name不太理解:
#define dbg(...) \ dbg_macro::DebugOutput(__FILE__, __LINE__, __func__, #__VA_ARGS__) \ .print(dbg_macro::type_name<decltype(__VA_ARGS__)>(), (__VA_ARGS__)) 还往各位大神试点迷津。
