据我了解,C规范说该类型int应该是目标平台上包含至少16位的最有效类型.
那不就是C99的定义int_fast16_t吗?
也许他们只是为了保持一致而把它放在那里,因为int_fastXX_t需要另一个?
更新
总结下面的讨论:
示例: x86-64上的MSVC具有32位int,即使在64位系统上也是如此.MS选择这样做是因为太多人认为int总是正好是32位,因此很多ABI会破坏.但是,如果x86-64上的64位值更快,则int_fast32_t可能是64位数.(我不认为实际情况如此,但它只是证明了这一点)
如果我有一个绑定到结构的不可变变量,Rust通常不允许我改变结构的字段或拥有的子结构的字段.
但是,如果该字段是一个可变引用,Rust 将允许我改变引用的对象,尽管我的绑定是不可变的.
为什么允许这样做?它与Rust的不变性的正常规则不一致吗?
Rust 不会让我通过不可变引用做同样的事情,因此不可变引用具有与不可变引用不同的行为.
代码示例:
struct Bar {
val: i32,
}
struct Foo<'a> {
val: i32,
bar: Bar,
val_ref: &'a mut i32,
}
fn main() {
let mut x = 5;
{
let foo = Foo {
val: 6,
bar: Bar { val: 15 },
val_ref: &mut x
};
// This is illegal because binding is immutable
// foo.val = 7;
// Also illegal to mutate child structures
// foo.bar.val = 20;
// This is fine …Run Code Online (Sandbox Code Playgroud) 在较旧的C++ 98中,我不相信有任何好方法可以在初始化列表中重用临时结果来初始化对象的多个成员.
在较新版本的C++(11,14,17)中,这有没有改变?
请考虑以下代码:
//
// compileShaders()
//
// Takes a string containing the source code to possibly hundreds of shaders
// for an effect (In this case, there are only 2 shaders for this effect)
//
// Returns a vector of compiled bytecode for each shader in the effect
//
std::vector<std::string> compileShaders(
const std::string& sourceCode,
const RenderTargetLayout& layout);
//
// class ScaleDownEffect
//
// Scales image to 1/4 in each dimension by using two 1/2 passes
//
class ScaleDownEffect …Run Code Online (Sandbox Code Playgroud) 我的理解是,对常规 ol' short/int/long 类型的某些按位运算要么是依赖于实现的( | & ~ >> ),要么是未定义的( << )
然而,C99 引入了固定宽度的整数类型,并明确地将它们定义为没有填充位的二进制补码精确位。
这是否意味着所有按位运算对于提供它们的平台之间的那些类型来说都是明确定义和可移植的?
例如,这可以在 My Machine™ 上运行,但它保证可以运行吗?
#include <inttypes.h>
#include <stdio.h>
int main() {
uint16_t a = 0xffff;
int16_t b = *(int16_t*)(&a);
printf("%" PRId16 "\n", b);
// Prints '-1'
b <<= 4;
printf("%" PRId16 "\n", b);
// Prints '-16'
return 0;
}
Run Code Online (Sandbox Code Playgroud)