I know this question may have been asked before but hasn't answered the query I am posting below. I am trying to figure out why exactly the code below gives the error (and in other version of GCC, a warning) and esp. how to resolve this? I am an experienced C++ programmer, but sometimes you do need to seek for help. Any help is appreciated.
[ERROR]: cannot pass objects of non-trivially-copyable type ‘class std::basic_string’ through
///
/// This method does a printf type formatting.
///
template<typename ... Args>
static std::string string_format( const std::string& format, Args ... args )
{
size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
std::unique_ptr<char[]> buf( new char[ size ] );
snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
The error is with snprintf statement above.
WARNING on g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609:
warning: format not a string literal and no format arguments [-Wformat-security] size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1;' ^
ERROR on g++ (Raspbian 4.9.2-10) 4.9.2
error: cannot pass objects of non-trivially-copyable type ‘class std::basic_string’ through ‘...’ size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1;
Note: I wish to understand and resolve this but without providing a compiler flag setting. I understand this may have something to do with C compatibility with C++.
Thanks!
Aucun commentaire:
Enregistrer un commentaire