Skip to content
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

Changed locale retrieval way to a fancy one #2594

Merged
merged 1 commit into from
Nov 12, 2021

Conversation

toughengineer
Copy link
Contributor

This is a quick and dirty one, but I like it much, much better than #2592.
I hope I haven't screwed up anything.

And this one is not slowed down compared to the ugly one with lamdas and whatnot.


private:
union {
monostate dummy{};
Copy link
Contributor

@phprus phprus Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

monostate is C++17 only

I'm sorry!!
This is fmt::monostate, not std::monostate.

Copy link
Contributor Author

@toughengineer toughengineer Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's monostate from core.h.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{} seems unnecessary, please remove.

Copy link
Contributor Author

@toughengineer toughengineer Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking this is unnecessary.

But without monostate default initialized, this is kinda scary because you have a generally unintialized member, and invariants become kinda hard to follow.

All this is OK for experts, but I imagine that this is harder to follow for non-experts.
monostate has negligible overhead, but helps to figure out what's happening more easily.
That's why I added monostate member.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

monostate is fine but I don't think you need to initialize it explicitly at least because it has no state =).

Copy link
Contributor Author

@toughengineer toughengineer Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you never initialize monostate dummy, than there is no point in having it at all.

IMHO, initializing monostate dummy by default gives some understanding and peace of mind to the person trying to understand the code.

Initializing it by default says that the lifetime of the dummy object is started, and you need to explicitly do something to change it.

In this case when locale is initialized using placement new, it explicitly says that the lifetime of dummy ends and another object starts to occupy the storage simultaneously starting its lifetime.

If there was no monostate dummy it would work perfectly correctly, but then a person trying to understand the code must keep in mind that the union is generally uninitialized.

@toughengineer
Copy link
Contributor Author

Yep, I screwed up, but now it seems to be fixed.

@phprus
Copy link
Contributor

phprus commented Nov 11, 2021

I suggested PR #2586, which moves all locale-specific formatters into the tm_writer class.
I have an idea how to simplify the locale-specific selectors (this PR). I can implement it on top of the PR #2586.

@phprus
Copy link
Contributor

phprus commented Nov 11, 2021

No, my idea is perf slower :(

@vitaut
Copy link
Contributor

vitaut commented Nov 12, 2021

LGTM but please rebase.

@vitaut vitaut merged commit 094b66e into fmtlib:master Nov 12, 2021
@vitaut
Copy link
Contributor

vitaut commented Nov 12, 2021

Merged, thanks!

@toughengineer toughengineer deleted the get_locale branch November 12, 2021 20:50
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