2

Is there a way to determine whether a variable is of an arithmetic type or not?

  • arithmetic: integer, floating point numbers, boolean values
  • not arithmetic: pointers, references; e.g. strings, and probably all other implementation- and user-defined types. However if someone for some reason defines typedef int myInt myInt should be considered arithmetic as well (because it is!)

In a normal C++ program I could use std::is_arithmetic<T>::value, where T is the type I need to check, but this doesn't seem to work in the Arduino environment. (Am I wrong? If it works, could someone please tell me how to use it?)

I imagine I could make a list of allowed types, e.g. as described in this answer to a similar question (I'm referring to the second option, the Alternative). But given that I don't need to exactly know the type of T I was wondering whether there is a more elegant and flexible solution...

noearchimede
  • 436
  • 1
  • 7
  • 19
  • 1
    Why do you need that? In a normal sketch you already know yours types. –  Aug 18 '17 at 12:19
  • 4
    What are you *actually* trying to do? – Majenko Aug 18 '17 at 12:24
  • 1
    There are some ports of the std library for Arduinos, but I'm not sure if they have this in. You could just look at the code and work out how its done and port it yourself. – Code Gorilla Aug 18 '17 at 12:26
  • I simplified the function I'm working on: template bool myFunc (int a, T b) { if(a*b > 100) return true; } --- EDIT: sorry, I forgot that code won't format in comments... I hope it's simple enough to be readable. – noearchimede Aug 18 '17 at 12:27
  • @noearchimede In comments, put the code in backticks. Read this for more info: https://arduino.stackexchange.com/editing-help#comment-formatting – Johnny Mopp Aug 18 '17 at 12:32
  • @Majenko Actually I'm writing a set of functions (in a library) that operate on arithmetic values, and it doesn't make sense to call them with non-arithmetic types (it's like writing pow("two", "four"). However given that I'm using the library in many different projects, and also other people will use it, I would like to be able to catch such stupid errors (either at compile time or at runtime, that makes no difference (those functions all print text, so they could print "not a valid number")). If I can't do that I'll write a long list of overloaded functions instead of using templates. – noearchimede Aug 18 '17 at 12:34
  • 2
    Ok, that makes sense. For Arduino code overloading is probably the simplest method. It's used throughout the core for things like `print()` etc. And you would only need to overload for the types you actually want to handle - then other types that don't "add up" you would just get a compilation error. – Majenko Aug 18 '17 at 12:51
  • I don't think Arduino IDE supports boolean arethmatic – tuskiomi Aug 18 '17 at 14:24
  • Thanks, I didn't think about that... but if there is an arduino-compatible way to determine whether a variable is of an arithmetic type it should automatically not include bool, right? – noearchimede Aug 18 '17 at 15:52

1 Answers1

1

Is there a way to determine whether a variable is of an arithmetic type or not?

no. end of the day, those are just bit pieces and it only makes sense to the computer with the right context. Another way to put it, the same bit pieces can and will mean different things under different situations.

dannyf
  • 2,730
  • 9
  • 13
  • I know that reading 4 bits representing a float as a long will give a valid but meaningless value. But the computer must somehow know how to interpret those bits, right? Either at compiletime or at runtime the type of each variable/set of bits must be known, and the property arithmetic/not arithmetic must also be known somehow: don't I get an error message if I try to do `myClass + aString`? (btw., may this be a solution to my problem? Try do do some arithmetics on the variables passed by the caller and output an error if it isn't possible to do so? Could that work, and if yes what could be... – noearchimede Aug 18 '17 at 19:19
  • ...an elegant implementation? (I mean, an error like "unable to do myclass+astring" wouldn't be very clear to someone who doesn't know the implementation of the function he is calling) – noearchimede Aug 18 '17 at 19:21