stdint.h和之间有什么区别cstdint?
它们都可以在MSVC(Visual Studio 2010)和gcc-4.5.1中使用.还定义了intX_t/ uintX_ttypes(其中X是类型的字节大小).
在stdint.h定义了每个类型没有任何名称空间,该cstdint类型在于std命名空间.
std命名空间中包含或不包含已定义的类型?这两个标题有什么不同?cstdint没有文件扩展名并使用c前缀,stdint.h使用.h扩展名.
c前缀表示这是一个C库?缺少文件扩展的原因是cstdint什么?我在使用C++类型特征时遇到了一些奇怪的行为,并将我的问题缩小到这个古怪的小问题,我将给出大量的解释,因为我不想留下任何误解的东西.
假设您有这样的程序:
#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;
}
Run Code Online (Sandbox Code Playgroud)
在使用GCC(以及32位和64位MSVC)进行32位编译时,程序的输出将为:
int: 0
int64_t: 1
long int: 0
long long int: 1
Run Code Online (Sandbox Code Playgroud)
但是,由64位GCC编译产生的程序将输出:
int: 0
int64_t: 1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译2007年编写的C++软件包,我收到了这个错误:
error: ‘uint32_t’ does not name a type
这是在使用g ++ 4.5.2的64位Ubuntu中发生的.它使用g ++ 4.1.2在64位CentOS上编译良好.
#include我缺少一个或一个编译器标志吗?或者,我应该typedef用来分配uint32_t一个size_t或者一个unsigned int?
我一直在思考是否应该在内部使用typedef <cstdint>.
我个人比较喜欢写uint32_t了unsigned int和int8_t在char等...因为这对我来说是一个很大更加直观.
你们有什么感想?是否使用typedef是一个好主意 <cstdint>?有什么缺点吗?
在 C++ 中,固定宽度整数被定义为optional,但我似乎无法找到推荐的方法来检查它们是否被实际定义。
检查固定宽度整数是否可用的便携式方法是什么?
为什么C++没有<cstdfloat>标头用于浮点数,就像它有整数的<cstdint>一样?
编辑:
通过<cstdfloat>我的意思是为float和double提供typedef的头文件.就像Qt中的qreal typedef一样.希望我的问题现在很明确.
Boost的C99 stdint实现非常方便.但有一件事让我感到困惑.他们将所有的typedef转储到boost namespace.这使我在使用此工具时有三个选择:
using namespace boost"using boost::[u]<type><width>_t"boost::前缀明确引用目标类型; 例如,boost::uint32_t foo = 0;boost::前缀常常≥所讨论的类型的长度.我的问题是:将所有这些类型引入全局命名空间的最优雅方法是什么?我应该只boost/cstdint.hpp使用选项№2 写一个包装器并用它完成吗?
此外,像这样包装标题在VC++ 10上不起作用(标准库标题的问题):
namespace Foo
{
#include <boost/cstdint.hpp>
namespace boost_alias = boost;
}
using namespace Foo::boost_alias;
Run Code Online (Sandbox Code Playgroud)
编辑:我想另一个选择是使用预处理器使其在VC 10上工作?以上片段为例:
#ifndef FOO_HPP_INCLUDED
#define FOO_HPP_INCLUDED
#if _MSC_VER >= 1600 /*VC++ 10*/ || defined USE_NATIVE_STDINT_HEADER
#include <stdint.h>
#else
namespace cstdint_wrapper
{
#include <boost/cstdint.hpp>
namespace boost_alias = boost;
}
using namespace cstdint_wrapper::boost_alias;
#endif
#endif …Run Code Online (Sandbox Code Playgroud) 我只是偶然发现了类型,u_int8_t因为它没有在Windows + MinGW中编译(但在Linux下编译得很好).根据该站点,C++ 11标准定义了类型uint8_t.我只是使用后者,一切正常.
出现的问题是:
u_int8_t和uint8_t?u_int8_t?uint8_t如果我使用C++ 11编译器(在不同的操作系统或架构上),可以安全地假设存在吗?intX_t和uintX_t)?当我尝试在 Windows 10 中编译 DOSBox SVN Daum 时,我遇到了 VS2015 中显然新出现的与标头相关的问题。示例:
Severity Code Description Project File Line Suppression State
Error (active) the global scope has no "int_least8_t" dosbox c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstdint 23
Error C2039 'int_least8_t': is not a member of '`global namespace'' dosbox C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstdint 23
Run Code Online (Sandbox Code Playgroud)
我的搜索告诉我,此类问题已经发生在周围的项目中,但我无法解决它。
特别是,我阅读了VisualStudio 2015 RC Issue with Includes和https://blogs.msdn.microsoft.com/vcblog/2015/03/03/introducing-the-universal-crt/,然后更改了 AppData\Local 的内容\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props 到:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>D:\dev\include;$(UniversalCRT_IncludePath);$(IncludePath)</IncludePath> …Run Code Online (Sandbox Code Playgroud) 似乎可以保证通过以下要求(已经在这里提出了要求):
#include <type_traits>
static_assert(!std::is_same_v<char, signed char>);
static_assert(!std::is_same_v<char, unsigned char>);
Run Code Online (Sandbox Code Playgroud)
[
char]与signed char或具有相同的表示和对齐方式unsigned char,但始终是不同的类型
难道这也保证了int8_t和uint8_t被在明确有符号类型来定义 未在来定义char的,因此也形成了一套3种不同类型的用char?
#include <cstdint>
#include <type_traits>
static_assert(!std::is_same_v<char, int8_t>);
static_assert(!std::is_same_v<char, uint8_t>);
Run Code Online (Sandbox Code Playgroud) #include <cstdio>
#include <cstdint>
#include <cassert>
int main() {
std::uint64_t ui;
char c;
auto ret = std::sscanf("111K", "%lu64%[B, K, M, G]", &ui, &c);
assert(ret == 2);
assert(ui == 111);
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用从一个字符串sscanf读取 auint64_t和 a ,但每次我尝试此操作时char它都只读取它ui(断言失败)。ret == 2
这是我的程序中的一段代码。我正在使用int8_t数据类型,并且在输入/输出方面遇到一些问题。显然,int8_t在程序中使用需要-std=c++11为编译器设置标志g++。我这样做了,但出现运行时错误。这是我的代码:
#include <iostream>
#include <cstdint>
using std::cout;
using std::cin;
using std::endl;
int main() {
int8_t number;
cout << "Enter a number [1-100]: ";
cin >> number;
cout << "You entered: " << number << endl;
cout << number << " / 10 = " << (number / 10) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是程序的输出:
$ ./a.out
Enter a number [1-100]: 95
You entered: 9
9 / 10 = 5
Run Code Online (Sandbox Code Playgroud)
这$是 …
我查看了<stdint.h>我的实现中的头文件.我看到以下内容:
typedef long int int_fast16_t;
typedef long int int_fast32_t;
typedef long int int_fast64_t;
Run Code Online (Sandbox Code Playgroud)
我有一个64位系统,所以long int占用64位.为什么所有三种数据类型都被定义为长整数?我理解int_fast64_t的情况,它是64位.但为什么16位和32位数据类型有64位?这是某种错误吗?我创建了一个小程序来检查是否是这种情况:
sizeof(int_fast8_t) : 1
sizeof(int_fast16_t) : 8
sizeof(int_fast32_t) : 8
sizeof(int_fast64_t) : 8
Run Code Online (Sandbox Code Playgroud)
是否定义了这些数据类型的大小?哪些特征或特征将数据类型定义为"快速"?是数据块从RAM加载到CPU的速度吗?如果int_fast16_t且int_fast32_t是8字节宽,性能有哪些好处?在64位系统上访问64位数据类型真的更快吗?