3 回答

TA貢獻1784條經驗 獲得超2個贊
我想建議我從Scott Meyer的“ Effective Modern C ++”中學到的boost :: typeindex,這是一個基本示例:
例
#include <boost/type_index.hpp>
class foo_bar
{
int whatever;
};
namespace bti = boost::typeindex;
template <typename T>
void from_type(T t)
{
std::cout << "\tT = " << bti::type_id_with_cvr<T>().pretty_name() << "\n";
}
int main()
{
std::cout << "If you want to print a template type, that's easy.\n";
from_type(1.0);
std::cout << "To get it from an object instance, just use decltype:\n";
foo_bar fb;
std::cout << "\tfb's type is : "
<< bti::type_id_with_cvr<decltype(fb)>().pretty_name() << "\n";
}
編譯為“ g ++ --std = c ++ 14”會產生以下結果
輸出量
如果要打印模板類型,這很容易。
T =兩倍
要從對象實例獲取它,只需使用decltype:
fb的類型是:foo_bar
- 3 回答
- 0 關注
- 521 瀏覽
添加回答
舉報