模板和 std::numeric_limits

发布于 2024-08-08 16:40:50 字数 457 浏览 3 评论 0原文

我有一个名为 Atomic 的类,它基本上是一个 _Atomic_word 加上调用 gcc 原子内置函数的方法。

class Atomic{
    mutable volatile _Atomic_word value_;
public:
    Atomic(int value = 0): value_(value) {}
    **** blah blah ****
};

我希望 std::numeric_limits 实例化为 std::numeric_limits (例如在我的系统上 _Atomic_word 是只是 int 的 typedef)。

有办法做到这一点吗?

I have a class called Atomic which is basically an _Atomic_word plus methods that call the gcc atomic builtins.

class Atomic{
    mutable volatile _Atomic_word value_;
public:
    Atomic(int value = 0): value_(value) {}
    **** blah blah ****
};

I would like std::numeric_limits<Atomic> to instantiate to std::numeric_limits<underlying integer type> (e.g. on my system _Atomic_word is just a typedef for int).

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-08-15 16:40:50

std::numeric_limits 将以 Atomic 作为类型进行实例化,您无法破坏它。但是,您可以将 std::numeric_limits 专门用于 Atomic,如下所示

template<>
class numeric_limits< Atomic > : public numeric_limits< Atomic::UnderlyingType >
{
};

,您显然将 UnderlyingType 公开为 Atomic 中的类型。

std::numeric_limits<Atomic> will instantiate with Atomic as the type, you can't subvert that. However you could specialise std::numeric_limits for Atomic like this

template<>
class numeric_limits< Atomic > : public numeric_limits< Atomic::UnderlyingType >
{
};

where you obviously expose UnderlyingType as a type in Atomic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文