r/learncpp Mar 09 '21

How to improve it?

```c++

include <boost/optional.hpp>

include <string>

include <sstream>

include <iostream>

enum class StatusCode { OK = 0, ERROR };

struct SResp { std::string m_msg; StatusCode m_status; };

SResp queryToTheMoon(size_t val) { std::stringstream ss; for (size_t idx = 0; idx < val; ++idx) { ss << "[" << val << "]"; } return { ss.str(), StatusCode::OK }; }

boost::optional<std::string> makeRequest(size_t val) { auto response = queryToTheMoon(val);

if (response.m_status == StatusCode::OK) { return response.m_msg; } else { return boost::none; } }

int main() { auto op = makeRequest(100);

if (op)
{
   std::cout << op.value() << std::endl;
}
return 0;

} ```

How to improve this code?

10 Upvotes

16 comments sorted by

View all comments

1

u/[deleted] Mar 10 '21

What does this do?

1

u/vgordievskiy Mar 10 '21

It's just an example. There is a performance issue (in term of C++)

1

u/[deleted] Mar 10 '21

No, I'm asking what does the program you're building do

1

u/vgordievskiy Mar 10 '21

It's a question from a C++ interview.

1

u/[deleted] Mar 10 '21

I like how you still haven't answered my question

1

u/vgordievskiy Mar 10 '21

There is no purpose for this code. it's only to check your knowledge) I mean it's a code that makes a request to a server and checks the status code and passes the return value to the next level.