Long long int 对比 C + + 中的 long int 对比 int64_t

在使用 C + + 类型 trait 时,我经历了一些奇怪的行为,并将我的问题缩小到这个古怪的小问题上,对此我将给出大量的解释,因为我不想留下任何可能被误解的东西。

假设你有这样一个程序:

#include <iostream>
#include <cstdint>


template <typename T>
bool is_int64() { return false; }


template <>
bool is_int64<int64_t>() { return true; }


int main()
{
std::cout << "int:\t" << is_int64<int>() << std::endl;
std::cout << "int64_t:\t" << is_int64<int64_t>() << std::endl;
std::cout << "long int:\t" << is_int64<long int>() << std::endl;
std::cout << "long long int:\t" << is_int64<long long int>() << std::endl;


return 0;
}

在使用 GCC (以及32位和64位 MSVC)的32位编译中,程序的输出将是:

int:           0
int64_t:       1
long int:      0
long long int: 1

然而,由64位 GCC 编译生成的程序将输出:

int:           0
int64_t:       1
long int:      1
long long int: 0

这很奇怪,因为 long long int是一个有符号的64位整数,而且从所有意图和目的来看,它与 long intint64_t类型相同,所以从逻辑上讲,int64_tlong intlong long int将是等价的类型——使用这些类型时生成的程序集是相同的。看看 stdint.h就知道为什么了:

# if __WORDSIZE == 64
typedef long int  int64_t;
# else
__extension__
typedef long long int  int64_t;
# endif

在64位编译器中,int64_tlong int,而不是 long long int(很明显)。

解决这种情况相当容易:

#if defined(__GNUC__) && (__WORDSIZE == 64)
template <>
bool is_int64<long long int>() { return true; }
#endif

但这是可怕的黑客和不良规模(实际功能的物质,uint64_t等)。有没有办法告诉编译器 long long int也是 int64_t,就像 long int一样?


我最初的想法是,由于 C/C + + 类型定义的工作方式,这是不可能的。没有一种方法可以指定基本数据类型与编译器的类型等价,因为这是编译器的工作(允许这样做可能会破坏很多东西) ,而 typedef只有一种方法。

I'm also not too concerned with getting an answer here, since this is a super-duper edge case that I do not suspect anyone will ever care about when the examples are not horribly contrived (does that mean this should be community wiki?).


Append : 我使用部分模板专门化的原因,而不是像下面这样简单的例子:

void go(int64_t) { }


int main()
{
long long int x = 2;
go(x);
return 0;
}

因为 long long int可以隐式转换为 int64_t,所以上面的例子仍然可以编译。


追加 : 到目前为止唯一的答案假设我想知道一个类型是否是64位的。我不想误导人们,让他们认为我关心这个问题,或许我应该提供更多的例子来说明这个问题在哪里表现出来。

template <typename T>
struct some_type_trait : boost::false_type { };


template <>
struct some_type_trait<int64_t> : boost::true_type { };

在这个例子中,some_type_trait<long int>将是 boost::true_type,而 some_type_trait<long long int>不是。虽然这在 C + + 的类型理念中是有意义的,但是并不可取。

另一个例子是使用像 same_type这样的限定符(在 C + + 0x 概念中很常用) :

template <typename T>
void same_type(T, T) { }


void foo()
{
long int x;
long long int y;
same_type(x, y);
}

该示例无法编译,因为 C + + (正确地)看到了类型的不同。G + + 将无法编译,出现如下错误: 没有匹配的函数调用 same_type(long int&, long long int&)

我想强调的是,我理解 为什么这是正在发生的,但我正在寻找一个变通方法,不强迫我重复代码所有的地方。

207430 次浏览

Do you want to know if a type is the same type as int64_t or do you want to know if something is 64 bits? Based on your proposed solution, I think you're asking about the latter. In that case, I would do something like

template<typename T>
bool is_64bits() { return sizeof(T) * CHAR_BIT == 64; } // or >= 64

You don't need to go to 64-bit to see something like this. Consider int32_t on common 32-bit platforms. It might be typedef'ed as int or as a long, but obviously only one of the two at a time. int and long are of course distinct types.

It's not hard to see that there is no workaround which makes int == int32_t == long on 32-bit systems. For the same reason, there's no way to make long == int64_t == long long on 64-bit systems.

If you could, the possible consequences would be rather painful for code that overloaded foo(int), foo(long) and foo(long long) - suddenly they'd have two definitions for the same overload?!

The correct solution is that your template code usually should not be relying on a precise type, but on the properties of that type. The whole same_type logic could still be OK for specific cases:

long foo(long x);
std::tr1::disable_if(same_type(int64_t, long), int64_t)::type foo(int64_t);

I.e., the overload foo(int64_t) is not defined when it's exactly the same as foo(long).

[edit] With C++11, we now have a standard way to write this:

long foo(long x);
std::enable_if<!std::is_same<int64_t, long>::value, int64_t>::type foo(int64_t);

[edit] Or C++20

long foo(long x);
int64_t foo(int64_t) requires (!std::is_same_v<int64_t, long>);

So my question is: Is there a way to tell the compiler that a long long int is the also a int64_t, just like long int is?

This is a good question or problem, but I suspect the answer is NO.

Also, a long int may not be a long long int.


# if __WORDSIZE == 64
typedef long int  int64_t;
# else
__extension__
typedef long long int  int64_t;
# endif

I believe this is libc. I suspect you want to go deeper.

In both 32-bit compile with GCC (and with 32- and 64-bit MSVC), the output of the program will be:

int:           0
int64_t:       1
long int:      0
long long int: 1

32-bit Linux uses the ILP32 data model. Integers, longs and pointers are 32-bit. The 64-bit type is a long long.

Microsoft documents the ranges at Data Type Ranges. The say the long long is equivalent to __int64.

However, the program resulting from a 64-bit GCC compile will output:

int:           0
int64_t:       1
long int:      1
long long int: 0

64-bit Linux uses the LP64 data model. Longs are 64-bit and long long are 64-bit. As with 32-bit, Microsoft documents the ranges at Data Type Ranges and long long is still __int64.

There's a ILP64 data model where everything is 64-bit. You have to do some extra work to get a definition for your word32 type. Also see papers like 64-Bit Programming Models: Why LP64?


But this is horribly hackish and does not scale well (actual functions of substance, uint64_t, etc)...

Yeah, it gets even better. GCC mixes and matches declarations that are supposed to take 64 bit types, so its easy to get into trouble even though you follow a particular data model. For example, the following causes a compile error and tells you to use -fpermissive:

#if __LP64__
typedef unsigned long word64;
#else
typedef unsigned long long word64;
#endif


// intel definition of rdrand64_step (http://software.intel.com/en-us/node/523864)
// extern int _rdrand64_step(unsigned __int64 *random_val);


// Try it:
word64 val;
int res = rdrand64_step(&val);

It results in:

error: invalid conversion from `word64* {aka long unsigned int*}' to `long long unsigned int*'

So, ignore LP64 and change it to:

typedef unsigned long long word64;

Then, wander over to a 64-bit ARM IoT gadget that defines LP64 and use NEON:

error: invalid conversion from `word64* {aka long long unsigned int*}' to `uint64_t*'