Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
repr.h File Reference

提供类似 Python 的 repr() 函数,用于生成对象的可读字符串表示。 More...

#include <iostream>
#include <typeinfo>
#include <memory>
#include <string>
#include "sfinae.h"

Go to the source code of this file.

Namespaces

namespace  console
 本库所有组件所在的顶层命名空间。

Functions

std::string console::tiname (const std::type_info &ti)
 获取类型信息的可读名称(跨平台 demangle)。
template<class T>
enable_if_string< T > console::repr (T &&value, std::ostream &os=std::cout)
 输出字符串类型(std::string, const char* 等)的表示,带双引号。
template<class T>
enable_if_char< T > console::repr (T &&value, std::ostream &os=std::cout)
 输出字符类型的表示,带单引号。
template<class T>
std::enable_if< std::is_same< typenamestd::decay< T >::type, bool >::value >::type console::repr (T &&value, std::ostream &os=std::cout)
 输出布尔值的表示("true" 或 "false")。
template<class T>
std::enable_if< std::is_same< typenamestd::decay< T >::type, std::nullptr_t >::value >::type console::repr (T &&, std::ostream &os=std::cout)
 输出 nullptr 的表示。
template<class Ret, class... Args>
void console::repr (Ret(*f)(Args...), std::ostream &os=std::cout)
 输出函数指针的表示,格式为 "<function at 地址>"。
template<class T>
std::enable_if<!std::is_same< typenamestd::decay< T >::type, bool >::value &&!is_string< typenamestd::decay< T >::type >::value &&!is_char< typenamestd::decay< T >::type >::value &&!std::is_function< typenamestd::decay< T >::type >::value &&is_printable< typenamestd::decay< T >::type >::value >::type console::repr (T &&value, std::ostream &os=std::cout)
 输出可打印类型(定义了 operator<<)的表示,直接输出值。
template<class T>
std::enable_if<!std::is_same< typenamestd::decay< T >::type, std::nullptr_t >::value &&!is_string< typenamestd::decay< T >::type >::value &&!is_char< typenamestd::decay< T >::type >::value &&!is_printable< typenamestd::decay< T >::type >::value >::type console::repr (T &&value, std::ostream &os=std::cout)
 输出不可打印类型的表示,格式为 "<类型名 object at 地址>"。

Detailed Description

提供类似 Python 的 repr() 函数,用于生成对象的可读字符串表示。

该头文件通过 SFINAE 技术重载 repr 函数,支持字符串、字符、布尔值、nullptr、函数指针、 可打印类型(有 operator<<)以及不可打印类型(输出类型信息及地址)。

Author
MrXie1109
Date
2026