Skip to content

[FIX] Add handling of composition of standard types #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

filipsajdak
Copy link
Contributor

@filipsajdak filipsajdak commented Oct 6, 2022

The current implementation does not allow to:

  • put variant, pair, tuple inside optional,
  • put pair, tuple inside variant,
  • put pair inside tuple

Below code will not compile:

p : std::pair<std::string_view, std::optional<std::string>> = ("first", std::nullopt);
std::cout << "p = (p)$\n";

t : std::tuple<double, std::optional<std::pair<std::string_view, int>>, std::optional<std::tuple<int, int, int>>> = (3.14, std::nullopt, std::nullopt);
std::cout << "t = (t)$\n";

vv : std::variant<int, std::string, std::pair<int, double> > = ();
std::cout << "vv = (vv)$\n";

This is just because the order of definitions of to_string specializations matters. When the compiler matches to_string for optional it is not aware of the existence of specializations for variant, pair, or tuple.

This change proposes to add forward declarations to to_string specializations for variant, pair, and tuple.

Current implementation does not allow to:
* put variant, pair, tuple inside optional,
* put pair, tuple inside variant,
* put pair inside tuple

This is just because order of definitions of to_string specialisations
matters. When compiler matches to_string for optional it is not
aware of existence of specialisations for variant, pair, tuple.

This change propose to add forward declarations to to_string
specialisations for variant, pair and tuple.
@JohelEGP
Copy link
Contributor

JohelEGP commented Oct 6, 2022

Reading https://en.cppreference.com/w/cpp/language/dependent_name, seems like qualifying with the calls with cpp2:: binds the lookup for to_string at the context of the template definition. Doesn't this mean that the last overload will never be used?

@hsutter hsutter merged commit f874f4c into hsutter:main Oct 6, 2022
hsutter added a commit that referenced this pull request Oct 6, 2022
And add regression tests for the compositions enabled by #49
@hsutter
Copy link
Owner

hsutter commented Oct 6, 2022

Thanks! I've added a followup commit to change the order of the last overload and to add the above test cases to the regression test example.

@filipsajdak
Copy link
Contributor Author

@JohelEGP Nice catch with the last overload - I completely ignored the fact that it should be matched. Thank you!

@filipsajdak filipsajdak deleted the fsajdak-string-interpolation-composition-of-standard-types branch January 12, 2023 21:21
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
…lation-composition-of-standard-types

[FIX] Add handling of composition of standard types
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
And add regression tests for the compositions enabled by hsutter#49
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.

3 participants