Ron*_*ean 7 c++ boost boost-format
我知道使用%s
格式说明符和std::string
这样会导致未定义的行为:
std::string myString = "test";
printf("%s", myString);
Run Code Online (Sandbox Code Playgroud)
但是,它保存到使用相同的符和std::string
使用boost::format
?
#include <boost/format.hpp>
int main()
{
std::string myString = "test";
boost::format fmt("%s");
fmt % myString;
std::cout << fmt.str();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
%s
指定一个(const)char*
,但我提供了一个std::string
.这会导致UB吗?
%s
与boost::format
和一起使用是安全的std::string
.与此相反printf
,在格式字符串中的字符类型"没有规定有关的论点是有限制的类型集合的,而仅仅是设置与此类型设定相关的标志."
http://www.boost.org/doc/libs/1_49_0/libs/format/doc/format.html#printf_directives