std
中是否有内置的检查器,该检查器检查类型是否可通过
operator<<
打印,或者是否可以通过std::format
。
我试图搜索它,找不到内置的检查器来检查是否可以在类型上调用。我只找到
operator<<
,无法弄清楚如何使用它。
我想从std::formattable<T>
使用内置类型的特征和概念,我不想创建自己的实用程序。我想做的是:
std
我试图搜索它,找不到内置的检查器来检查是否可以在类型上调用。我只找到
template<typename DATA_T>
struct std::formatter<MyClass<DATA_T>> : std::formatter<std::string>
{
static auto format(const MyClass<DATA_T>& my_class, std::format_context& ctx)
{
std::string result = std::format("Name: {}\n", my_class.name());
// Check if the data it holds printable or formattable and add it.
// Should be replaced with correct usage
if constexpr (std::formattable<DATA_T>)
{
result += std::format("Data: {}", my_class.data());
}
// Should be replaced with correct std functionality
else if constexpr (std::ostreambale<DATA_T>)
{
std::ostringstream oss;
oss << my_class.data();
result += "Data: ";
result += oss.str();
}
result += std::format("\nLocation: {}", my_class.location());
return std::format_to(ctx.out(), "{}", result);
}
};
(((请参见LiveDemostd::formattable<T>
在此更新中,您可能会这样做:
operator<<
))