区域设置构面构造函数被忽略

2023-12-11

The locale Facet构造函数:

构造 other 的副本,但从参数 Facet 安装的 Facet 类型的 Facet(通常从参数的类型推导)除外。如果facet为NULL,则构造的语言环境是其他语言环境的完整副本。以这种方式构建的语言环境没有名称。

我尝试使用我的构建Facet here,但是当我在我的do_decimal_point and do_thousands_sep他们从来没有被称为:(

我可以看到Facet被传递,但它被传递到标准库实现文件中,所以我看不到是否用它做了任何事情。

我已经在 Visual Studio 2013、Clang 3.6.0 和 gcc 4.9.2 上尝试过此操作。All他们中的一些人表现得好像我从来没有通过过Facet只是使用另一个locale的行为。

我在任何编译器中都找不到针对此构造函数的任何错误。我认为我正在以正确的方式做这件事。为什么我无法获取locale使用我的构建Facet?

EDIT:

At 0x499602D2的请求我添加了一个例子。有趣的是,Facet does似乎被拾起但是not与...一起使用get_money。我正在链接一个活生生的例子(这必然使用locale("C")代替locale("en-US")):

class Foo : public std::moneypunct<char> {
protected:
    char_type do_decimal_point() const {
        cout << "Hit Foo::do_decimal_point";
        return ',';
    }
    char_type do_thousands_sep() const {
        cout << "Hit Foo::do_thousands_sep";
        return '.';
    }
};

int main()
{
    cout.imbue(locale(locale("en-US"), new Foo));

    const moneypunct<char>* temp = &use_facet<std::moneypunct<char>>(cout.getloc());

    cout << temp->decimal_point() << endl << temp->thousands_sep() << endl;

    istringstream USCurrency("1,234.56 -1,234.56 1.234,56 -1.234,56");
    USCurrency.imbue(cout.getloc());

    long double value;

    USCurrency >> get_money(value, true);

    return 0;
}

这输出:

击中 Foo::do_thousands_sepHit Foo::do_decimal_point,
.

我希望它输出:

击中 Foo::do_thousands_sepHit Foo::do_decimal_point,
.
点击 Foo::do_thousands_sepHit Foo::do_decimal_point

EDIT2:

看起来moneypunct<char>不能继承,因为它没有正确构造,除非它是由内部构造的locale。至少在 Visual Studio 上这是一个问题,因为它决定是否使用thousands_sep by the grouping。解决方法可能是完全重新实现moneypunct<char>的功能。我现在正在修改这个。与此同时,我还在这里添加了一个错误:https://connect.microsoft.com/VisualStudio/feedback/details/1524749/inheriting-from-moneypunct-requires-use-of-unavailable-construction-information


事实上,do_decimal_place and do_thousands_place are受到尊重get_money。困难在于事实上moneypunct继承自默认构造,因此支持信息直接get_money打电话do_decimal_place and do_thousands_place没有被设置。

Visual Studio 的实现moneypunct提供了两个公共构造函数:

  1. moneypunct()
  2. moneypunct(const _Locinfo& _Lobj, size_t _Refs = 0, bool _Isdef = false)

locale的构造函数调用第二个moneypunct构造函数。创建一个合适的_Locinfo是问题的关键,因为该信息似乎是特定于实现的。这链接的 Visual Studio Bug请求一种构造函数的方法moneypunct无法访问实施细节。代替此信息的所有moneypunct田地必须煮熟。

由于这个问题是关于延长预期工作moneypunct最简单的方法是使用赋值运算符或复制构造函数。坏消息:两者都被删除。所以punct_facet(const money_punct&)需要在内部编写实现复制构造函数的行为。需要复制的值对应于所有需要重写的虚函数punct_facet。最后你的类将看起来类似于:

template <typename T>
class punct_facet : public T {
protected:
    typename T::string_type m_grouping;
    typename T::string_type m_curr_symbol;
    typename T::string_type m_positive_sign;
    typename T::string_type m_negative_sign;
    int m_frac_digits;
    typename T::pattern m_pos_format;
    typename T::pattern m_neg_format;

    typename T::char_type do_decimal_point() const {
        return typename T::char_type(',');
    }

    typename T::char_type do_thousands_sep() const {
        return typename T::char_type('.');
    }

    typename T::string_type do_grouping() const {
        return m_grouping;
    }

    typename T::string_type do_curr_symbol() const {
        return m_curr_symbol;
    }

    typename T::string_type do_positive_sign() const {
        return m_positive_sign;
    }

    typename T::string_type do_negative_sign() const {
        return m_negative_sign;
    }

    int do_frac_digits() const {
        return m_frac_digits;
    }

    typename T::pattern do_pos_format() const {
        return m_pos_format;
    }

    typename T::pattern do_neg_format() const {
        return m_neg_format;
    }
public:
    punct_facet(const T& defaultFacet) : m_grouping(defaultFacet.grouping()),
                                         m_curr_symbol(defaultFacet.curr_symbol()),
                                         m_positive_sign(defaultFacet.positive_sign()),
                                         m_negative_sign(defaultFacet.negative_sign()),
                                         m_frac_digits(defaultFacet.frac_digits()),
                                         m_pos_format(defaultFacet.pos_format()),
                                         m_neg_format(defaultFacet.neg_format()) {}
};

EDIT:

这个解决方案是跨平台的,但它也不能令人满意,因为必须将所有成员添加到punct_facet 已经存在 in moneypunct。我不知道有什么干净的解决方法可以解决这种问题。此处提供了特定于编译器的 hack:https://stackoverflow.com/a/31454039/2642059

这将导致punct_facet鉴于 Visual Studio 将 v-table 指针放置为对象布局中的第一项,看起来更像是这样:

template <typename T>
class punct_facet : public T {
private:
    void Init(const T* money){
        const auto vTablePtrSize = sizeof(void*);

        memcpy(reinterpret_cast<char*>(this) + vTablePtrSize, reinterpret_cast<const char*>(money) + vTablePtrSize, sizeof(T) - vTablePtrSize);
    }
protected:
    typename T::char_type do_decimal_point() const {
        return typename T::char_type(',');
    }

    typename T::char_type do_thousands_sep() const {
        return typename T::char_type('.');
    }
public:
    punct_facet(){
        Init(&use_facet<T>(cout.getloc()));
    }

    punct_facet(const T* money){
        Init(money);
    }
};

顺便说一句,这个实现punct_facetClang 3.6.0 不支持,但是isgcc 5.1.0 支持:http://coliru.stacked-crooked.com/a/e4a1d88b560d6d1b

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

区域设置构面构造函数被忽略 的相关文章

随机推荐

  • 将 3D 模型安装在窗口内

    我想显示适合视图的不同尺寸的模型 以便整个模型在屏幕内可见 最好的方法是什么 我尝试使用此公式缩放 使用 glScale 模型 scaleFactor screenSize maxModelSize constant 其中尺寸是高度或宽度
  • 隐藏 Woocommerce 中特定用户角色的特定运输方式

    在 Woocommerce 中 我使用的是 WooCommerce Wholesale Pro Suite 来自IgniteWoo 以及统一费率盒子运输插件 将 B2B 添加到我们的网上商店 我正在尝试禁用特定用户角色 客人和客户的统一费率
  • Rails map.resources 与 has_many :through 不起作用?

    我有三个 相关的 模型 指定如下 class User lt ActiveRecord Base has many posts has many comments has many comments received through gt
  • .NET Maui - 重置 TabBar 项目单击上的导航

    我目前正在使用 NET Maui 想知道选项卡内的导航是如何工作的 我在文档中找不到我要找的内容 但如果我错过了 也许有人可以为我指出 所以目前我有一个带有两个底部选项卡的选项卡栏 第二个选项卡显示项目列表 当我单击一个项目时 我想显示一个
  • 使用 jq 将 JSON 转换为 CSV

    我有一个 json 文件 它存储在环境变量 temp 中 users username jack email email protected total running apps 1 api mock app 0 flogo 1 ipaas
  • Java 约定:在类中使用 getter/setter?

    我的教授非常强调通过始终使用访问器和修改器来访问私有实例变量来防止隐私泄露 但是 我是否必须在类中使用类的 getter setter 方法 举例来说 如果我有以下课程 public class Person private String
  • 使用 Anaconda 在 Google App Engine 上安装软件包?

    这个周末我制作了一个 Flask 应用程序 它使用了很多 ML 包 比如 Pytorch 模型都已经构建好了 所以我们不需要像 Google Compute Engine 这样疯狂的东西 但是 我仍然需要安装这些库 然而 其中许多 例如 P
  • 使用executeOnExecuter可以并行执行多少个?

    在我的应用程序中 当用户点击列表中的文档时 我需要提供文档的下载功能 因此 对于多个活动线程 AsyncTask 进入了我的脑海 但我也注意到人们并没有推荐 AsyncTask 来进行这种操作 因为在这种情况下大号没有 线程将在点击文档时开
  • @EnableMongoAuditing for MongoDB on Cloud Foundry / mongolab

    我的设置在本地有效 但在将其部署到 CloudFoundry mongolab 时无效 配置非常类似于docs 我本地的 spring 配置 Configuration Profile dev EnableMongoAuditing Ena
  • 这段 JavaScript 代码 getElementById 有什么问题吗?

    我下面有这个代码 div This is an example div 为什么它不起作用 该脚本在具有给定 id 的元素存在之前运行 并且您有一个DOM 属性名称其中有一个连字符 被视为减号运算符 div This is an exampl
  • 在express中全局重定向所有尾随斜杠

    我正在使用 Node js 和 Express 并且有以下路由 app get function req res locals date new Date toLocaleDateString res render home ejs loc
  • 一次不能多次借用“x”作为可变的

    在下面的代码中 操场 struct Node datum static str edges Vec
  • 从日期时间熊猫中提取季节

    我正在尝试从带有日期时间列的大型数据框中提取季节 这是我使用过的代码 def season of date date UTC year str date UTC year seasons spring pd date range start
  • ConcurrentModificationException 仅在 Java 1.8.0_45 中

    我对这段代码有两个问题 import java util public class TestClass private static List
  • JNI无法释放内存

    这是 JNI 的 C 代码 extern C JNIEXPORT jbyteArray JNICALL Java cn rilled encoder JNIEncoder encodeEncryptBuff JNIEnv env jobje
  • preg_split 逗号不在括号内

    测试字符串 Organic whole wheat bread Monterey Jack Cheese milk cheese culture salt Hormel Natural Ham salt turbinado sugar la
  • 处理字节顺序和文件的最常见方法 C++

    我一开始只是使用字符读取 写入 8 位整数到文件 不久之后 我意识到我需要能够处理的可能值不仅仅是 256 个 我对如何读取 写入 16 位整数到文件进行了一些研究 并了解了大端和小端的概念 我做了更多的研究 发现了一些不同的方法来处理字节
  • 如何在 AngularJS 中编写去抖动服务

    underscore 库提供了防抖功能 可以防止在设定的时间内多次调用某个函数 他们的版本使用了 setTimeout 我们如何在纯 AngularJS 代码中做到这一点 此外 我们可以利用 q 风格的 Promise 在去抖期之后从被调用
  • 泽西岛客户端 API 问题

    我正在编写一个调用 Web 服务的 Android 客户端 该服务是使用 JAX RS Jersey API 编写的 我还尝试在 android 端使用 Jersey Client API Client client Client crea
  • 区域设置构面构造函数被忽略

    The locale Facet构造函数 构造 other 的副本 但从参数 Facet 安装的 Facet 类型的 Facet 通常从参数的类型推导 除外 如果facet为NULL 则构造的语言环境是其他语言环境的完整副本 以这种方式构建