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

Cannot override float processing #1353

Closed
lexxmark opened this issue Oct 9, 2019 · 2 comments · Fixed by #1360
Closed

Cannot override float processing #1353

lexxmark opened this issue Oct 9, 2019 · 2 comments · Fixed by #1360

Comments

@lexxmark
Copy link

lexxmark commented Oct 9, 2019

Tried to override format for standard types according to documentation:

`
using arg_formatter = fmt::arg_formatter<fmt::back_insert_rangefmt::internal::buffer>;

class na_arg_formatter : public arg_formatter {
public:
na_arg_formatter(fmt::format_context &ctx, fmt::format_specs *spec = nullptr)
: arg_formatter(ctx, spec),
m_na_formatter(ctx)
{}

using arg_formatter::operator();

auto operator()(double value) {
	if (IsNA(value))
		return m_na_formatter("NA");
	return arg_formatter::operator()(value);
}

auto operator()(float value) {
	if (IsNA(value))
		return m_na_formatter("NA");
	return arg_formatter::operator()(value);
}

auto operator()(unsigned value) {
	if (IsNA(value))
		return m_na_formatter("NA");
	return arg_formatter::operator()(value);
}

auto operator()(int value) {
	if (IsNA(value))
		return m_na_formatter("NA");
	return arg_formatter::operator()(value);
}

private:
arg_formatter m_na_formatter;
};

inline std::string na_vformat(fmt::string_view format_str, fmt::format_args args) {
fmt::memory_buffer buffer;
// Pass custom argument formatter as a template arg to vformat_to.
fmt::vformat_to<na_arg_formatter>(buffer, format_str, args);
return fmt::to_string(buffer);
}

template <typename ...Args>
inline std::string na_format(
fmt::string_view format_str, const Args &... args) {
return na_vformat(format_str, fmt::make_format_args(args...));
}
`

Float overloading is not working.
Double case is chosen.
na_format("{}", 4.f);

@vitaut
Copy link
Contributor

vitaut commented Oct 9, 2019

This is effectively a subset of the issue reported in #1336 - we should stop promoting float to double.

@vitaut
Copy link
Contributor

vitaut commented Oct 12, 2019

Fixed in #1360.

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 a pull request may close this issue.

2 participants