标签: cstdint

<cstdint> vs <stdint.h>

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++ cstdint stdint

83
推荐指数
2
解决办法
5万
查看次数

C++:long long int vs. long int vs. int64_t

我在使用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)

c++ gcc cstdint

81
推荐指数
2
解决办法
15万
查看次数

'uint32_t'没有命名类型

我正在尝试编译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

c++ cstdint uint32-t

66
推荐指数
4
解决办法
15万
查看次数

我应该使用cstdint吗?

我一直在思考是否应该在内部使用typedef <cstdint>.

我个人比较喜欢写uint32_tunsigned intint8_tchar等...因为这对我来说是一个很大更加直观.

你们有什么感想?是否使用typedef是一个好主意 <cstdint>?有什么缺点吗?

c++ cstdint

40
推荐指数
2
解决办法
1万
查看次数

如何检查是否定义了固定宽度的整数

在 C++ 中,固定宽度整数被定义为optional,但我似乎无法找到推荐的方法来检查它们是否被实际定义。

检查固定宽度整数是否可用的便携式方法是什么?

c++ types cstdint stdint c++11

27
推荐指数
2
解决办法
1558
查看次数

为什么我们在C++中没有<cstdfloat>?

为什么C++没有<cstdfloat>标头用于浮点数,就像它有整数的<cstdint>一样?

编辑:

通过<cstdfloat>我的意思是为float和double提供typedef的头文件.就像Qt中的qreal typedef一样.希望我的问题现在很明确.

c++ cstdint

6
推荐指数
1
解决办法
1891
查看次数

提升的"cstdint"用法

Boost的C99 stdint实现非常方便.但有一件事让我感到困惑.他们将所有的typedef转储到boost namespace.这使我在使用此工具时有三个选择:

  1. 使用" using namespace boost"
  2. 使用" using boost::[u]<type><width>_t"
  3. boost::前缀明确引用目标类型; 例如,boost::uint32_t foo = 0;

  • 选项№1种打败命名空间点.即使在局部范围内使用(例如,在函数内),函数参数之类的东西仍然必须像选项№3那样加前缀.
  • 选项№2更好,但有一堆这些类型,所以它可以变得嘈杂.
  • 选项№3增加了极端的噪音; 的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)

c++ boost wrapper cstdint

5
推荐指数
1
解决办法
7698
查看次数

C++ 11 uint类型vs u_int

我只是偶然发现了类型,u_int8_t因为它没有在Windows + MinGW中编译(但在Linux下编译得很好).根据该站点,C++ 11标准定义了类型uint8_t.我只是使用后者,一切正常.

出现的问题是:

  1. 有什么区别u_int8_tuint8_t
  2. 有没有理由(除了遗留代码)u_int8_t
  3. uint8_t如果我使用C++ 11编译器(在不同的操作系统或架构上),可以安全地假设存在吗?
  4. 上述问题的答案是否也适用于其他类型(intX_tuintX_t)?

uint cstdint c++11

5
推荐指数
1
解决办法
5928
查看次数

VS 2015 C头错误C2039:'int_least8_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 Includeshttps://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)

c include dosbox cstdint visual-studio-2015

5
推荐指数
1
解决办法
2176
查看次数

标准是否保证uint8_t,int8_t和char都是唯一类型?

似乎可以保证通过以下要求(已经在这里提出了要求):

#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)

引用cppreference

[ char]与signed char或具有相同的表示和对齐方式unsigned char,但始终是不同的类型

难道这也保证了int8_tuint8_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)

c++ char language-lawyer cstdint

5
推荐指数
2
解决办法
92
查看次数

为什么 sscanf 无法从一个字符串中读取 uint64_t 和 char?

#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

c++ char cstdint cstdio

1
推荐指数
1
解决办法
2683
查看次数

C++ 11 int8_t 有问题的输入/输出

这是我的程序中的一段代码。我正在使用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)

$是 …

printf iostream input cstdint c++11

0
推荐指数
1
解决办法
1892
查看次数

为什么64位系统上的int_fast16_t为64位?

我查看了<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_tint_fast32_t是8字节宽,性能有哪些好处?在64位系统上访问64位数据类型真的更快吗?

c cstdint stdint

-2
推荐指数
1
解决办法
131
查看次数