简体   繁体   English

覆盖std :: locale上的多个方面

[英]Override more than one facet on std::locale

Is it possible to override more than one facet on std::local ? 是否有可能在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 locale(std::locale(), new NumberFacet());

How would I pass MoneyFacet as well? 我还将如何通过MoneyFacet

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? 有更好的方法吗?

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. IOStreams库没有为您提供更好的编写方法,但是您可以利用递归来完成工作。 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 /*, ... */>);
}

operator<<() fits this case: operator<<()适合这种情况:

#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 . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM