7

When attempting to convert a std::string to an Aws::String using the following code:

std::string s("Johnny is cool");
Aws::String aws_s(s);

And I get the following error:

error: no matching function for call to ‘std::__cxx11::basic_string<char, std::char_traits<char>, Aws::Allocator<char> >::basic_string(const string&)’
johnnyodonnell
  • 1,838
  • 3
  • 16
  • 34

1 Answers1

10

From https://github.com/aws/aws-sdk-cpp/issues/416. Thanks Bu11etmagnet!


If you have a std::string you want to pass to an Aws function, construct an Aws::String from it

std::string s{"whatever"};
Aws::String aws_s(s.c_str(), s.size());
Aws::SomeFunction(aws_s);

If you got an Aws::String from an Aws function, construct a std::string from it:

Aws::String const& aws_s = Aws::SomeFunction();
std::string s(aws_s.c_str(), aws_s.size());

Both of these perform a copy of the string content, unfortunately.

Aws::String aws_s(std_string.c_str()) would measure the string length with strlen(), an information already contained in the std::string object.

johnnyodonnell
  • 1,838
  • 3
  • 16
  • 34
  • 1
    The important point probably isnt't the inefficiency of re-calculating the length, but the possibility of embedded zeroes. – Deduplicator Sep 21 '18 at 23:53
  • Will the string aws_s be null terminated though? – user2962885 Jan 25 '19 at 19:16
  • `aws_s` here is an object of type `Aws::String`, which is [defined](https://sdk.amazonaws.com/cpp/api/1.9.342/_a_w_s_string_8h_source.html#l00097) in the AWS C++ SDK to be a kind of `std::basic_string`. As [such](https://en.cppreference.com/w/cpp/string/basic_string), `aws_s.c_str()` should [return](https://en.cppreference.com/w/cpp/string/basic_string/c_str) a C-style null-terminated string. – webninja Sep 10 '22 at 00:30