Using the% s format specifier with forcing :: format and std :: string

I know that using the %s format specifier and std::string , as this leads to undefined behavior:

 std::string myString = "test"; printf("%s", myString); 

But is it possible to use the same qualifier and std::string with boost::format ?

 #include <boost/format.hpp> int main() { std::string myString = "test"; boost::format fmt("%s"); fmt % myString; std::cout << fmt.str(); return 0; } 

%s indicates (const) char* , but I provide std::string . Could this lead to UB?

+7
source share
1 answer

It is safe to use %s with boost::format and std::string . Unlike printf , the type symbol in the format string "does not impose appropriate arguments on a limited set of types, but simply sets the flags associated with this specification type."

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

+8
source

All Articles