template<typename... Ts>
class dumpType;
int func(int, int) {
int x;
return x;
}
class Base {
public:
int x = 0;
};
int main() {
const Base b;
const int ci = 1;
auto x = ci;
using T1 = decltype(x);
using T2 = decltype((x));
using T3 = decltype(b.x);
using T4 = decltype((b.x));
using T5 = decltype(func);
dumpType<T1, T2, T3, T4, T5>{};
}
编译时将输出以下的错误信息:
error: implicit instantiation of undefined template
'dumpType<int, int &, int, const int &, int (int, int)>'
auto add (auto p1, auto p2) { return p1 + p2; };
auto d = add(1, 2.0);
printf("type of d is %s\n", typeid(d).name());
auto s = add("hello"s, "world"s);
printf("type of s is %s\n", typeid(s).name());
输出的结果是:
type of d is d
type of s is NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
#include <iostream>
template<typename... Ts>
void dumpType() {
// GCC/Clang使用这行
std::cout << __PRETTY_FUNCTION__ << std::endl;
// MSVC则使用下面这行
//std::cout << __FUNCSIG__ << std::endl;
};
int func(int, int) {
int x;
return x;
}
class Base {
public:
int x = 0;
};
int main() {
const Base b;
const int ci = 1;
auto x = ci;
using T1 = decltype(x);
using T2 = decltype((x));
using T3 = decltype(b.x);
using T4 = decltype((b.x));
using T5 = decltype(func);
dumpType<T1, T2, T3, T4, T5>();
}