Zero Bit Types
For some types, @sizeOf is 0:
- void
- The Integers
u0
andi0
. - Arrays and Vectors with len 0, or with an element type that is a zero bit type.
- An enum with only 1 tag.
- A struct with all fields being zero bit types.
- A union with only 1 field which is a zero bit type.
- Pointers to Zero Bit Types are themselves zero bit types.
These types can only ever have one possible value, and thus require 0 bits to represent. Code that makes use of these types is not included in the final generated code:
export fn entry() void {
var x: void = {};
var y: void = {};
x = y;
}
When this turns into machine code, there is no code generated in the body of entry
, even in Debug mode. For example, on x86_64:
0000000000000010 <entry>:
10: 55 push %rbp
11: 48 89 e5 mov %rsp,%rbp
14: 5d pop %rbp
15: c3 retq
These assembly instructions do not have any code associated with the void values - they only perform the function call prologue and epilog.
void
void
can be useful for instantiating generic types. For example, given a Map(Key, Value)
, one can pass void
for the Value
type to make it into a Set
:
test.zig
const std = @import("std");
const assert = std.debug.assert;
test "turn HashMap into a set with void" {
var map = std.HashMap(i32, void, hash_i32, eql_i32).init(std.debug.global_allocator);
defer map.deinit();
_ = try map.put(1, {});
_ = try map.put(2, {});
assert(map.contains(2));
assert(!map.contains(3));
_ = map.remove(2);
assert(!map.contains(2));
}
fn hash_i32(x: i32) u32 {
return @bitCast(u32, x);
}
fn eql_i32(a: i32, b: i32) bool {
return a == b;
}
$ zig test test.zig
1/1 test "turn HashMap into a set with void"...OK
All tests passed.
Note that this is different from using a dummy value for the hash map value. By using void
as the type of the value, the hash map entry type has no value field, and thus the hash map takes up less space. Further, all the code that deals with storing and loading the value is deleted, as seen above.
void
is distinct from c_void
, which is defined like this: pub const c_void = @OpaqueType();
. void
has a known size of 0 bytes, and c_void
has an unknown, but non-zero, size.
Expressions of type void
are the only ones whose value can be ignored. For example:
test.zig
test "ignoring expression value" {
foo();
}
fn foo() i32 {
return 1234;
}
$ zig test test.zig
/home/andy/dev/zig/docgen_tmp/test.zig:2:8: error: expression value is ignored
foo();
^
However, if the expression has type void
, there will be no error. Function return values can also be explicitly ignored by assigning them to _
.
test.zig
test "void is ignored" {
returnsVoid();
}
test "explicitly ignoring expression value" {
_ = foo();
}
fn returnsVoid() void {}
fn foo() i32 {
return 1234;
}
$ zig test test.zig
1/2 test "void is ignored"...OK
2/2 test "explicitly ignoring expression value"...OK
All tests passed.
Pointers to Zero Bit Types
Pointers to zero bit types also have zero bits. They always compare equal to each other:
test.zig
const std = @import("std");
const assert = std.debug.assert;
test "pointer to empty struct" {
const Empty = struct {};
var a = Empty{};
var b = Empty{};
var ptr_a = &a;
var ptr_b = &b;
comptime assert(ptr_a == ptr_b);
}
$ zig test test.zig
1/1 test "pointer to empty struct"...OK
All tests passed.
The type being pointed to can only ever be one value; therefore loads and stores are never generated. ptrToInt and intToPtr are not allowed:
test.zig
const Empty = struct {};
test "@ptrToInt for pointer to zero bit type" {
var a = Empty{};
_ = @ptrToInt(&a);
}
test "@intToPtr for pointer to zero bit type" {
_ = @intToPtr(*Empty, 0x1);
}
$ zig test test.zig
/home/andy/dev/zig/docgen_tmp/test.zig:5:20: error: pointer to size 0 type has no address
_ = @ptrToInt(&a);
^
/home/andy/dev/zig/docgen_tmp/test.zig:5:9: note: referenced here
_ = @ptrToInt(&a);
^
/home/andy/dev/zig/docgen_tmp/test.zig:9:19: error: type '*Empty' has 0 bits and cannot store information
_ = @intToPtr(*Empty, 0x1);
^
/home/andy/dev/zig/docgen_tmp/test.zig:9:9: note: referenced here
_ = @intToPtr(*Empty, 0x1);
^