0

I'm introducing new types in my program, such as tileno_i, pageno_i and blockno_u where I used to have only int or unsigned, the idea being to communicate more clearly what sort of value a function takes or returns.

I had hope to use g++'s -Wconversion flag to spot places where some local variables should be converted to the new types as well, but I end up with lots of situations requiring useless casting, such as

for (tileno_i i = 0 ; i < size(); i += 4) {
//                                  ^
// warning: conversion to tileno_i {aka short unsigned int} from 'int' may alter its value

Is there a flag similar to Wconversion that would let me catch errors without going to assume that things are automatically promoted to ints as soon as I do arithmetic on them?

Niall
  • 30,036
  • 10
  • 99
  • 142
PypeBros
  • 2,607
  • 24
  • 37
  • Whether you assume it or not, promotions happen, as part of the arithmetic conversions. – Kerrek SB Feb 18 '18 at 10:33
  • agreed, but it makes tracking these new types impossible within the ton of warning coming from external headers. I'd also like to avoid killing the readability with extra casts all over the place. Plus, if I always end up with `var = (tileno_t) arg + 1`, that wouldn't check that arg was a tileno_t in first place. – PypeBros Feb 18 '18 at 10:40
  • 1
    That might say something about the design of those new types. – Kerrek SB Feb 18 '18 at 10:40
  • Please not that POSIX reserves all type names ending in `_t` for the implementation, so naming your own types like that is ill adviced. – Jesper Juhl Feb 18 '18 at 11:16
  • 1
    Have a look at Boost's Stong typedef see: https://stackoverflow.com/questions/9211210/using-strong-typedef-as-a-more-lightweight-alternative-to-boost-parameter-librar – Richard Critten Feb 18 '18 at 13:46
  • What does `4u` give you? – Niall Feb 19 '18 at 20:41
  • @Niall: it says "conversion to 'tileno_t {aka short unsigned int} from 'unsigned int' may alter its value". Unfortunately. – PypeBros Feb 19 '18 at 21:44
  • Sorry, forgot there is no suffix for `short` - don't think a custom one will help either – Niall Feb 19 '18 at 21:55
  • Why use `short`s anyway? – Niall Feb 19 '18 at 21:57
  • tiles number are ultimately written into 16-bit, low-level video memory areas. So it (should) be better to keep them in u16 (short unsigned). Target platform is NintendoDS, 4MiB data, 16KiB stack. – PypeBros Feb 19 '18 at 22:20

1 Answers1

0

My best workaround so far is to temporarily typedef xxx_t to something else than integers (e.g. an enum) so that the 'type errors' due to not-yet-converted lines trigger something at a higher error level than what requires -Wconversion.

PypeBros
  • 2,607
  • 24
  • 37