Reputation: 5352
Is it possible to override more than one facet on std::local
?
For instance, taking a rather contrived example:
#include <locale>
class NumberFacet : public std::numpunct<char>
{
protected:
char do_decimal_point() const override
{
return '.';
}
char do_thousands_sep() const override
{
return ',';
}
};
class MoneyFacet : public std::moneypunct<char>
{
protected:
char do_decimal_point() const override
{
return '.';
}
char do_thousands_sep() const override
{
return ',';
}
};
I know I can override one facet of std::locale
like this to create a new locale
variable.
std::locale locale(std::locale(), new NumberFacet());
How would I pass MoneyFacet
as well?
It seems unsatisfactory that I would have to do this:
std::locale locale(std::locale(std::locale(), new NumberFacet()), new MoneyFacet());
Is there a nicer way?
Upvotes: 2
Views: 662
Reputation: 4997
operator<<()
fits this case:
#include <locale>
template <typename T,
typename = std::enable_if_t<
std::is_constructible<std::locale, std::locale, T*>::value
>
>
std::locale operator<<(const std::locale& locale, T* facet)
{
return {locale, facet};
}
int main()
{
auto locale = std::locale{} << new MoneyFacet{} << new NumberFacet{};
std::cout.imbue(locale);
std::cout << 12345.67f << '\n';
std::cout << 123456789u << '\n';
}
Demo.
Upvotes: 3
Reputation: 96835
The IOStreams library doesn't provide you with a better way of writing that, but you can take advantage of recursion to get the job done. Since imbuing a new locale always involves copying from an old locale, you can continuously recurse to build a new locale with the provided facets.
template<class...>struct types{constexpr types(){}};
template<class...Ts>constexpr types<Ts...> types_t{};
template<class L, class F>
L makeloc(L loc, types<F>, int) {
return std::locale(loc, new F{});
}
template<class L, class F, class... Fs>
L makeloc(L loc, types<F, Fs...>, long) {
return makeloc(std::locale(loc, new F{}), types_t<Fs...>, 0);
}
template<class S, class L, class... Fs>
void imbue(S& s, L loc, types<Fs...> facets) {
s.imbue(makeloc(loc, facets, 0));
}
int main() {
imbue(std::cout, std::locale(), types_t<MoneyFacet, NumberFacet /*, ... */>);
}
Upvotes: 2