【问题标题】:Declare an "unsigned T"声明一个“无符号 T”
【发布时间】:2013-03-26 05:32:16
【问题描述】:

我想在模板中使用无符号 T:

template<class T>
void signed2unsigned(const T* source, unsigned T* dest, size_t n)
    {
    do
        {
        intmax_t s=*source;
        s+=min(*source);

        *dest=s;

        ++dest;
        ++source;
        --n;
        }
    while(n!=0);
    }

这不起作用,因为在考虑类型 T 之前, unsigned without 已扩展为 unsigned int。除了引入 U 并在文档中写入 U 必须是无符号 T 之外,还有其他解决方法吗?

【问题讨论】:

标签: c++ templates integer unsigned


【解决方案1】:

你可以使用std::make_unsigned&lt;T&gt;::type

【讨论】:

  • 除了 g++ (tdm64-1) 4.7.1 在 template void signed2unsigned(const T* source, std::make_unsigned::type * dest, size_t n) 并且我有 -std=c++11 on 给出 'std::make_unsigned::type' 不是类型
  • 使用 typedef std::make_unsigned::type T_type
  • 尽管它可以编译,但它实际上打破了使用模板的想法,因为我无法将 typedef 作为模板。
  • @user877329 您想将dest 限制为类似无符号的类型(uint8_tuint16_tuint32_tuint64_t)?
  • 好吧,我认为这是一个新问题的主题
【解决方案2】:

std::make_unsigned&lt;T&gt;::type 似乎是您正在寻找的。​​p>

【讨论】:

    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2017-11-28
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多