如何检查类型是格式化的'使用类型特征 /概念?
我想检查是否可以与 std ::格式
一起使用某种类型。
这是我天真的尝试:
template<typename Object>
concept formattable = requires(const Object & obj)
{
std::format("{}", obj);
};
但这不起作用。对于所有类型,它基本上返回true。甚至那些不能与std ::格式一起使用的人。
static_assert(!格式&lt; std :: vector&lt; int&gt;&gt;); //返回false
为什么不起作用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
随着,C ++ 23中已经有一个概念,用于测试类型是否格式,即
std :: formattable
。由于
std ::格式
不是受约束的函数,因此expressionstd ::格式(“ {}”,obj),obj)
始终是良好的。您可能想做的主要基于
basic_format_arg
的构造方法的约束 [format.arg] 。demo
Update:
With the introduction of Formatting Ranges, there is already a concept in C++23 dedicated to testing whether a type is formattable, namely
std::formattable
.Since
std::format
is not a constrained function, the expressionstd::format("{}", obj)
is always well-formed. You might want to dowhich mainly based on the constraints of the
basic_format_arg
's constructor in [format.arg].Demo