English 中文(简体)
跨平台 C++ 确定最大值(无头主)
原标题:Cross-platform C++ determining maximim integer value (no headers)

根据编组结构,C++行使职能或结构、类别(使用元数据编制)确定签字和未签字类型的最高价值。 1人签名,2人未签名。

所需经费:

  • no header files
  • self adjusting to variable sizes (no stdint.h)
  • no compiler warnings about possible overflow

Clarification:

After comment s I am惊讶, on response for non norm C++ problem. 我已学到,最好强调,问题不是家庭工作,而不是从月球上,而是实际领域。

对于所有有兴趣应用这一障碍的人来说,首先,这不是家务劳动: 参看practical,根据我面临的实际问题回答问题,如SO。 建议采用。 感谢您对<代码>climits等表示的感谢,但我正在寻找“聪明法典”。 定有<代码>climits,limits,经过良好测试和精良的代码,但它们是巨大的,不一定是“智慧、trick”。 我们在这里寻求聪明的解决办法(不是“传统-任何”解决办法),是我们的 t? 即便是,<代码>climits中的建议作为起点。 对于那些对包括头盔档案在内的地区感兴趣的人来说,以及源码的规模是相关的,很少有:与汇编者进行的实验、方案转变、编制方案竞赛的问题等。 他们的实际树种与我目前正在挣扎的问题有关。 因此,我想到的是(SO.FAQ)too localized ,我认为,它肯定会问:(SO.FAQ)enthusiast programmers。 如果你认为,即便如此,这个问题也存在一些错误,请让我知道——我不想再犯错误。 如果说是ok,请让我知道,我能做些什么才能避免 down倒?

最佳回答

如果你希望将签名的封顶与“不签名”合并起来,则可以在编辑时确定签名。

#include <iostream>
#include <cstddef> // for pytdiff_t, intptr_t

template <typename T> static inline  bool is_signed() { 
    return ~T(0) < T(1);
}
template <typename T> static inline  T min_value() {
    return is_signed<T>() ? ~T(0) << (sizeof(T)*8-1) : T(0); 
}
template <typename T> static inline  T max_value() { 
    return ~min_value<T>();
}

#define REPORT(type) do{ std::cout
    << "<" #type "> is " << (is_signed<type>() ? "signed" : "unsigned")
    << ", with lower limit " << min_value<type>()
    << " and upper limit " << max_value<type>()
    << std::endl; }while(false)

int main(int argc, char* argv[]) {
    REPORT(char);  // min, max not numeric
    REPORT(int);
    REPORT(unsigned);
    REPORT(long long);
    REPORT(unsigned long long);
    REPORT(ptrdiff_t);
    REPORT(size_t);
    REPORT(uintptr_t);
    REPORT(intptr_t);
}
问题回答

根据两个补充代表的合理假设:

template<typename T> struct maxval;

template<> struct maxval<unsigned char>
{
  static const unsigned char value = (unsigned char) ~0;
};

template<> struct maxval<signed char>
{
  static const signed char value = ((unsigned char) ~0) >> 1;
};

template<> struct maxval<unsigned short>
{
  static const unsigned short value = (unsigned short) ~0;
};

template<> struct maxval<short>
{
  static const short value = ((unsigned short) ~0) >> 1;
};

int
main ()
{
  std::cout << (int)maxval<signed char>::value << std::endl;
}

同样,其他类型也是如此。

在确定最高价值时,需要区分已签署和未签署类型。 简便的方法就是列举上述例子中的所有内容。

或许可以通过以下几种组合进行:enable_if std:is_unsign,但实施(无头盔!)仍需要列举所有类型。

对于未签字的类型,其简单之处是:T(-1)将始终是这种类型的最高限度(-1被削减为最适合范围的最大模块,总是给予该类型的最大上限)。

For signed integer types, the job is almost as easy, at least in practice: take the maximum unsigned value, shift right one bit, and cast to signed. For C99 or C++11 that will work because only three representations for integers (1 s complement, signed magnitude and 2 s complement) are allowed (and it gives the correct result for all three). In theory, for C89/90 and/or C++98/03, it might be possible to design a conforming signed type for which it would fail (e.g., a biased representation where the bias was not range/2).

对这些人和浮动点类型(没有签名的对口单位)来说,工作难度更大。 这样做的理由不是要由你自己来计算。

Edit: As far as how to implement this for in C++, most of the difficulty is in specializing a template for an unsigned type. The most obvious way to do that is probably to use SFINAE, with an expression that will only be legal for a signed type (or only for an unsigned type). The usual for that would be an array whose size is something like T()-1>0. This will yield false for a signed type, which will convert to 0; since you can t create a zero-sized array, that attempted substitution will fail. For an unsigned type, the -1 will "wrap" to the maximum value, so it would create a size of 1, which is allowed.

由于这似乎属于家务劳动,因此我不想显示实际的、工作上的执行。

This works for unsigned types:

template <typename t>
constexpr t max_val() { // constexpr c++11 thing, you can remove it for c++03
  return ~(t(0));
}

由于你可以假定借方和编码的数量,因此可以找到签名。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?