Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert between a std::string and an Aws::String?

Tags:

c++

aws-sdk

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&)’
like image 857
johnnyodonnell Avatar asked Sep 21 '18 23:09

johnnyodonnell


1 Answers

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.

like image 131
johnnyodonnell Avatar answered Oct 04 '22 20:10

johnnyodonnell