0

I am taking a look at an Open Source repo with this file written in C++. The two functions use lambdas and callbacks, and I just want to better understand the order of operations and how they work together.

How is this block executed when WindowScenarioselectOpen(scenarioselect_callback callback) is called?

Here is the code block:

WindowBase* WindowScenarioselectOpen(scenarioselect_callback callback)
{
    return WindowScenarioselectOpen([callback](std::string_view scenario) { callback(std::string(scenario).c_str()); });
}

WindowBase* WindowScenarioselectOpen(std::function<void(std::string_view)> callback)
{
    auto* window = static_cast<ScenarioSelectWindow*>(WindowBringToFrontByClass(WindowClass::ScenarioSelect));
    if (window != nullptr)
    {
        return window;
    }

    int32_t screenWidth = ContextGetWidth();
    int32_t screenHeight = ContextGetHeight();
    ScreenCoordsXY screenPos = { (screenWidth - WW) / 2, std::max(TOP_TOOLBAR_HEIGHT + 1, (screenHeight - WH) / 2) };
    window = WindowCreate<ScenarioSelectWindow>(WindowClass::ScenarioSelect, screenPos, WW, WH, 0, callback);
    return window;
}

Also, scenarioselect_callback is defined in another file as using scenarioselect_callback = void (*)(const utf8* path);.

To my understanding, the call to the first windowscenarioselect defined a lambda that is passed into the second one. This lambda takes in a string_view scenario, which is used to construct a string, then converted to a C string with c_str , which is then passed into the scenarioselect_callback definition.

My confusion is I thought we can't construct a string from a string_view, and another question is, can c_str's return which is a char* be used as a utf8*?

I am not so interested in the second function with a longer body, I am more interested in how the arguments are set up and work in order to make the second function work.

monk234
  • 1
  • 1
  • Related https://stackoverflow.com/questions/59424390/how-to-correctly-create-stdstring-from-a-stdstring-view – john Jul 01 '23 at 20:56
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/4641116) – Eljay Jul 01 '23 at 21:06
  • please improve the title of the question – alfC Jul 01 '23 at 21:26

1 Answers1

0

My confusion is I thought we can't construct a string from a string_view,

There is no implicit conversion from a std::string_view to a std::string, this is true. However an explicit conversion is allowed:

std::string to_string(const std::string_view &sv)
{
    return std::string{sv};
}

An attempt to summon an implicit conversion with a plain return sv; won't work here, but an explicit construction is allowed. And does what's expected: a resulting std::string containing the same sequence as the defined std::string_view.

can c_str's return which is a char* be used as a utf8*?

Nope. For starters, c_str() returns a const char * and not a char *.

another file as using scenarioselect_callback = void ()(const utf8 path);

Details matter. A const utf8 * is specified here, not utf8 *. A brief search of the C++ standard does not appear to find any mention of a type called utf8 that's defined in the C++ library.

Occam's razor concludes that utf8 is just your application code's typedef for a char, so you really have a const char * here, which c_str() is more than happy to return.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148