Skip to content

Conversation

@sylveon
Copy link
Contributor

@sylveon sylveon commented Oct 17, 2022

This introduces a new API, winrt::format, the exact equivalent of std::format (including compile-time format string checking), that efficiently formats to an hstring by using hstring_builder. It avoids the copies that would be required if using std::format.

Note that this will only light up on VS 17.5 Preview 1 (which is not available to the public yet), but I've manually tested it on VS 17.4 previews by changing std::wformat_string to std::_Fmt_wstring (the internal name before it became a public API).

@kennykerr
Copy link
Collaborator

Note that this will only light up on VS 17.5 Preview 1 (which is not available to the public yet)

Would this work on Clang? I'm reluctant to merge this in until we can test it.

@sylveon
Copy link
Contributor Author

sylveon commented Oct 17, 2022

It depends on changes in the STL, but it should work in Clang once VS is updated.

@kennykerr
Copy link
Collaborator

I meant we could use Clang to test it so long, but yes if its STL-dependent then we'd have to wait for MSVC to catch up.

@sylveon
Copy link
Contributor Author

sylveon commented Oct 17, 2022

Clang also uses the MSVC STL on Windows, so it's also blocked on 17.5 preview 1. I'm totally okay with waiting for a preview release before merging, I just opened this because it's work I had already done.

@kennykerr
Copy link
Collaborator

It's very cool - similar to Rust's format so this is lovely. ❤️

@github-actions
Copy link

This pull request is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@sylveon
Copy link
Contributor Author

sylveon commented Oct 29, 2022

Commenting to avoid closing. This is stale because it's blocked on VS 17.5

@github-actions
Copy link

This pull request is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@sylveon
Copy link
Contributor Author

sylveon commented Nov 10, 2022

Going to look at this soon, VS 17.5 preview is out now

@sylveon
Copy link
Contributor Author

sylveon commented Nov 10, 2022

Just took a look: this works fine on 17.5 Preview 1 with no changes needed 🎉

@kennykerr
Copy link
Collaborator

Looks good - I assume the CI build isn't yet validating this as its still using a previous version of the compiler. Is that correct?

@DefaultRyan did you want to take a look at this?

@sylveon
Copy link
Contributor Author

sylveon commented Nov 11, 2022

I would assume so as well, since 17.5 is preview only at the moment.

@kennykerr kennykerr merged commit 104b0b9 into microsoft:master Nov 12, 2022
@sylveon
Copy link
Contributor Author

sylveon commented Nov 17, 2022

@kennykerr any chance of making a new release? I'd like to start using this

@kennykerr
Copy link
Collaborator

Coming up! (2.0.221117.1)

@ChrisGuzak
Copy link
Member

ChrisGuzak commented Nov 27, 2022

fyi, wil has std::str_printf, but does not support winrt::hstring.

@jaigak
Copy link
Contributor

jaigak commented Dec 14, 2022

Could you add winrt::format for older versions using the internal API (on MSVC at least) or simply being a wrapper on std::format that copies the result into an hstring?

@sylveon
Copy link
Contributor Author

sylveon commented Dec 14, 2022

No, because the STL team strongly recommended against using the internal std::_Format_string (so we cannot just wrap std::format either, being able to wrap it was the entire reason for std::format_string to be made public), and std::vformatted_size would be required if we decided to just take a normal std::wstring_view for the format string, but that API does not exist.

You need to use VS 17.5 for this. The entire point of winrt::format was to keep the compile time checks, so having them only work sometimes is not great.

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.

5 participants