Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 2.15 KB

278.md

File metadata and controls

115 lines (81 loc) · 2.15 KB
Info

Example

static_assert(-42 == -42z);
static_assert(-42 == -42Z);
static_assert(42 == 42uz);
static_assert(42 == 42uZ);
static_assert(42 == 42Uz);
static_assert(42 == 42ZU);
static_assert(42 == 42Zu);

static_assert(std::is_same_v<std::size_t, decltype(42uz)>);

https://godbolt.org/z/Wxe9Kfze3

Puzzle

  • Can you make the following snippets compatible with 32/64 bit arch?
int main() {
    std::vector v{1, 2, 3};

    for (auto i = 0; i < v.size(); ++i) {
    }

    for (auto i = 0, s = std::size(v); i < s; ++i) {
    }

    std::max(0, std::ssize(v));
    std::min(0, std::size(v));
	  std::max(0, std::min(0, v.size()));
}

https://godbolt.org/z/EEdfz5451

Solutions

for (auto i = 0uz; i < v.size(); ++i) {
}

for (auto i = 0uz, s = std::size(v); i < s; ++i) {
}

std::max(0z, std::ssize(v));
std::min(0uz, std::size(v));
std::max(0uz, std::min(0uz, v.size()));

https://godbolt.org/z/dfTbhx1r7

#include <array> // Change vector to array for elision of generated code. :)

int main() {
  std::array v{1, 2, 3};

  for (auto i = 0uz; i < v.size(); ++i) {
  }

  for (auto i = 0uz, s = std::size(v); i < s; ++i) {
  }

  std::max(0z, std::ssize(v));
  std::min(0uz, std::size(v));
	std::max(0uz, std::min(0uz, v.size()));
}

https://godbolt.org/z/GhdW1Ebo8

int main() {
  std::vector v{1, 2, 3};

  for (auto i = 0uz; i < v.size(); ++i) {
  }

  for (auto i = 0uz, s = std::size(v); i < s; ++i) {
  }

  std::max(0z, std::ssize(v));
  std::min(0uz, std::size(v));
	std::max(0uz, std::min(0uz, v.size()));
}

https://godbolt.org/z/KeKKPG7cv

int main() {
  std::vector v{1, 2, 3};

  for (auto i = 0UZ; i < v.size(); ++i) {
  }

  for (auto i = 0UZ, s = std::size(v); i < s; ++i) {
  }

  std::max(0Z, std::ssize(v));
  std::min(0UZ, std::size(v));
	std::max(0UZ, std::min(0UZ, v.size()));
}

https://godbolt.org/z/xxqzE1cee