Skip to content

Conversation

filipsajdak
Copy link
Contributor

Specialisation of cpp2::to_string function for std::optional is using std::to_string on optional value.

template<typename T>
auto to_string(std::optional<T> const& o) -> std::string {
    if (o.has_value()) {
        return std::to_string(o.value());
    }
    return "(empty)";
}

That makes the below code fail to compile:

std::optional<std::string> v { "this will not work" }; 
std::court << "(v)$" << std::endl;

cppfront will generate

std::optional<std::string> v { "this will not work" }; 
std::cout << "My map: " + cpp2::to_string(v) + "\n" << std::endl;

The result is that there is a call to std::to_string(std::string) and there is no such specialization in the standard library.

Closes #38

@filipsajdak filipsajdak force-pushed the fsajdak-fix-to_string-for-optional branch 2 times, most recently from 228835a to 0fd2904 Compare October 1, 2022 21:21
@filipsajdak
Copy link
Contributor Author

I have added examples of use in regression tests and results of execution.

Calling std::to_string makes it impossible to use to_string
on optional that contains std::string.

The solution is to call cpp2::to_string instead of std::to_string.

Added examples of use in regression tests with results of execution.
@hsutter hsutter self-assigned this Oct 5, 2022
@filipsajdak filipsajdak force-pushed the fsajdak-fix-to_string-for-optional branch from f7c4c40 to ba82d87 Compare October 5, 2022 18:32
@hsutter hsutter closed this in a5c3dc3 Oct 5, 2022
@filipsajdak filipsajdak deleted the fsajdak-fix-to_string-for-optional branch January 12, 2023 21:21
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] cpp2::to_string(std::optional<T>) is calling std::to_string
3 participants