- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面的代码
#include <cstdio>
#include <cstdint>
#include <type_traits>
int main()
{
static const char* b2s[] = { "no", "yes" };
printf( "%s\n", b2s[std::is_same<unsigned long, uint64_t>::value] );
}
返回 yes
在 Linux 上编译时,no
在 OSX 上编译时。
我一边阅读一边尝试理解为什么这是 apparently normal .如果我正在开发一个库并且我希望它是可移植的,我该如何处理这个问题?我是否必须处理每个操作系统的偏好?
这是一个例子:
foo.h
#include <cstdint>
template <class T>
struct Foo
{
static const char id;
};
foo.cpp
#include "foo.h"
#define DEFINE_FOO(type_,id_) \
template <> const char Foo<type_>::id = id_; \
template <> const char Foo<const type_>::id = id_;
DEFINE_FOO( bool,'a')
DEFINE_FOO( int8_t,'b')
DEFINE_FOO( uint8_t,'c')
DEFINE_FOO( int16_t,'d')
DEFINE_FOO(uint16_t,'e')
DEFINE_FOO( int32_t,'f')
DEFINE_FOO(uint32_t,'g')
DEFINE_FOO( int64_t,'h')
DEFINE_FOO(uint64_t,'i')
DEFINE_FOO( float,'j')
DEFINE_FOO( double,'k')
// OSX requires this, but Linux considers it a re-definition
// DEFINE_FOO(unsigned long,'l')
作为我的库的一部分,当我需要创建一个可执行文件(比如 main.cpp
)时,前一个被编译然后链接。通常,这看起来像:
$(CC) -c -Wall -std=c++0x -o foo.o foo.cpp
$(CC) -Wall -std=c++0x -o main main.cpp foo.o
这将在一个平台上运行但在另一个平台上失败;如果我取消注释 DEFINE_FOO(unsigned long,'l')
在foo.cpp
然后 OSX 很高兴,但 Linux 说 Foo<uint64_t>
正在被重新定义。反之亦然。
这怎么可能是正常的?有没有一种“便携”的方式来处理这个问题?
最佳答案
为了使其可移植,我将宏调用包含在 #ifdef
中 根据目标操作系统的条件使用 this list ,例如:
#ifdef __linux__
DEFINE_FOO(uint64_t,'l')
#endif
[...]
#ifdef __APPLE__
DEFINE_FOO(unsigned long,'l')
#endif
请注意,我将 uint64_t
设置为 l
,因为 unsigned long
和 uint64_t
在 64 位架构中几乎是等效的使用 gcc。
关于c++ - OSX 与 Linux : how to deal with unsigned long and uint64_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814040/
我不知道下面的代码有什么问题,它应该读取数字并将它们的值与位置放在一个成对的 vector 中,然后对它们进行排序并打印出位置。我用 sort 删除了部分 - 我认为问题就在那里,但我再次收到编译错误
我相信当您将两个 unsigned int 值相加时,返回值的数据类型将是 unsigned int。 但是两个 unsigned int 值相加可能会返回一个大于 unsigned int 的值。
我从左移得到的结果我找不到解释。 unsigned char value = 0xff; // 1111 1111 unsigned char = 0x01; // 0000 0001 std::c
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
根据:http://en.wikipedia.org/wiki/C_data_types您可以使用 unsigned short 类型或 unsigned short int 类型。但是它们之间有什么
我正在尝试在 arduino 草图中实现 CRC16。从网上拿到了crc.c文件,想试试看。我创建了其他文件以允许 crc.c 正确运行。这是我的文件。 crc.h: #ifndef CRC_C_ #
我正在尝试实现一个将存储相关索引的列表。但是,我在标题中提到的 for (index_itr = (list_size - numberOfEvents - 1) 处遇到错误。我在做什么错误,以及如何
这不是跨平台代码...所有内容都在同一平台上执行(即字节序是相同的......小字节序)。 我有这个代码: unsigned char array[4] = {'t', 'e', 's', '
我有一个 8 位 unsigned char vector 和一个 16 位 unsigned short vector std::vector eight_bit_array; std::vecto
这个问题在这里已经有了答案: Is subtracting larger unsigned value from smaller in C++ undefined behaviour? (2 个答案
这个问题已经有答案了: Difference between unsigned and unsigned int in C (5 个回答) 已关闭 6 年前。 在 C 语言中,unsigned 之间有
我遇到了一个我不太理解的警告。该警告是通过比较我认为是一个未签名的内容与另一个未签名的内容而生成的。 来源如下: #include #include #include #include int
好吧,这一定很愚蠢。我在移动一些代码时遇到了这个问题,并认为我打错了字或未能正确使用调试器。作为健全性检查,我创建了这个测试用例,但它似乎仍然失败。 unsigned int vtxIdx
我有一个同事不热衷于使用现代 C++ 例如,当我要求他开始使用 r_value 引用时,他不会这样做。当我要求他使用 std::array 而不是 c 数组(char example[8])时,他不会
我有一个无符号字符数组,例如Data[2]。我需要它来与返回 unsigned int 的函数的输出进行比较。 我尝试将 Data[2] 转换为 unsigned int,反之亦然。它没有用。 我想做
我遇到了一个我不太明白的警告。警告是通过将我认为是未签名的与另一个未签名的进行比较而生成的。 这是来源: #include #include #include #include int mai
在下面的程序中,我使用了 unsigned 关键字。 #include int main() { unsigned char i = 'A'; unsigned j
整数值转换为浮点值并再次返回时是否与原始整数值相同? 例如: unsigned x = 42; double y = x; unsigned z = y; 假设编译器没有优化浮点转换,x == z 是
这个问题在这里已经有了答案: Difference between unsigned and unsigned int in C (5 个答案) 关闭 9 年前。 我理解 unsigned 和 un
您好,我遇到了一个关于位运算的小概念问题。请参阅下面的代码,其中我有一个 4 字节的无符号整数。然后我通过将地址分配给无符号字符来访问各个字节。 然后我将最后一个字节的值设置为 1。并对 unsign
我是一名优秀的程序员,十分优秀!