【问题标题】:How to resolve templated-types from instances in C++如何从 C++ 中的实例解析模板类型
【发布时间】:2019-08-30 16:26:52
【问题描述】:

当我查看 STL chrono 库的 GNU 实现时,我看到:

    /// seconds
    typedef duration<int64_t>           seconds;

我想从duration 类型中解码int64_t(特别是类型,而不是值),以进行进一步的静态评估。

有没有办法执行模板元编程来实现这一点?

编辑:我对持续时间的值不感兴趣,我只关心用于表示它的类型。

【问题讨论】:

  • “解码”是什么意思?
  • 你可能想要rep
  • std::chrono::milliseconds mils = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(sec) ???
  • 我不知道rep,看起来他们就是我要找的东西。可以静态测试吗?
  • std::is_integral&lt; std::chrono::seconds::rep &gt; ?

标签: c++ templates types template-meta-programming


【解决方案1】:
template<class T> struct HeadArg;
template<template<class...> class Ctor, class H, class... More>
struct HeadArg<Ctor<H, More...>> {
    using type = H;
};

【讨论】:

  • std::chrono::duration 有一个成员类型 rep。您无需求助于模板专业化技巧。
  • 不幸的是,问题的标题比这要宽一些。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多