使用带有boost :: format和std :: string的%s格式说明符

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吗?

nos*_*sid 8

%sboost::format和一起使用是安全的std::string.与此相反printf,在格式字符串中的字符类型"没有规定有关的论点是有限制的类型集合的,而仅仅是设置与此类型设定相关的标志."

http://www.boost.org/doc/libs/1_49_0/libs/format/doc/format.html#printf_directives