Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« on: November 02, 2012, 12:25:16 am » |
Hello all,
If there any way that I can write a function that will accept multiple datatypes? This is what I am trying to do: I want a function that I can pass either a string of characters or an integer or an object and it will treat it as a stream of bytes so that I can write them out to the serial port one byte at a time. The function will also include things like byte stuffing, but I can deal with that later.
I remember that there was an EEPROMWriteAnything function that used template <class> and things like that but I can't figure out how to adapt it to my needs. I tried and all I get is errors; things like "expected ',' or '...' before '&' token" and "error: ISO C++ forbids declaration of 'T' with no type"
Help?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 3
Posts: 812
|
 |
« Reply #1 on: November 02, 2012, 12:42:39 am » |
All you neediest function overloading. Write both functions andngive them the same name and the compiler will figure it out based on the data type of the argument.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 119
Posts: 10172
|
 |
« Reply #2 on: November 02, 2012, 12:46:28 am » |
Certainly.... void SendIt( void * const raw, unsigned size ) { uint8_t const * rover;
rover = (unit8_t const *) raw; while ( count > 0 ) { Serial.write( *rover ); ++rover; --count; } }
SendIt( &MyInt, sizeof(MyInt) );
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: November 02, 2012, 01:09:36 am » |
I tried and all I get is errors; things like "expected ',' or '...' before '&' token" and "error: ISO C++ forbids declaration of 'T' with no type"
That's so sad, I can't tell you. Read this before posting a programming questionWhat code? What errors? <sigh> Not "things like". Copy and paste the errors.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 11
Posts: 393
|
 |
« Reply #4 on: November 02, 2012, 01:24:44 am » |
Hello all,
If there any way that I can write a function that will accept multiple datatypes?
Function overloading is really just syntactic sugar for having multiple functions with the same name but different formal parameter lists. Name mangling behind the scenes ensures the linker can tell them apart, but they are all really completely different functions, of course. If you really want just a single function, you could consider passing a void pointer to your function that holds the address of your variable/array/structure. You would have to pass a second argument as well to tell the function how many bytes the variable/array/structure holds. Defining a macro using sizeof() might be a neat solution for passing the second argument automatically. Less coding and less flash memory than overloading. Edit: Oops, just saw Coding Badly already did this. Must read more carefully before posting next time!
|
|
|
|
« Last Edit: November 02, 2012, 01:29:36 am by pico »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: November 02, 2012, 01:31:45 am » |
Please don't. Passing void basically defeats type checking. And then all the extra code to work out what type it has received will negate any savings of only having one function.
You now have one complex function that isn't type safe, rather than two simple functions which are.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 11
Posts: 393
|
 |
« Reply #6 on: November 02, 2012, 01:45:46 am » |
Passing void basically defeats type checking.
Yes. That's the point.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #7 on: November 02, 2012, 02:10:56 am » |
The point is, you don't want to defeat type checking. Why do you think compiler-writers built that in?
|
|
|
|
|
Logged
|
|
|
|
|
Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #8 on: November 02, 2012, 02:13:21 am » |
That's so sad, I can't tell you. Apologies, Nick. Those were actually the errors I got when I tried to compile my sketch with the EEPROMWriteAnything function in it. Word for word, copied and pasted. I just didn't make it clear. Sorry. Just finished a night shift. So, which approach should I use? Passing a void pointer or writing multiple functions? Chris
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: November 02, 2012, 02:18:33 am » |
Multiple functions. If you have problems post your code.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #10 on: November 02, 2012, 02:19:17 am » |
However I don't have any objections to templates, which, in effect, produce multiple functions.
|
|
|
|
|
Logged
|
|
|
|
|
Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #11 on: November 02, 2012, 02:20:39 am » |
How do templates work? I have tried googleing but I can't seem to find a nice concise guide.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 11
Posts: 393
|
 |
« Reply #12 on: November 02, 2012, 02:25:57 am » |
The point is, you don't want to defeat type checking.
Yes. Sometimes you do. Why do you think compiler-writers built that in?
Equally, why do you think they built in void pointer? Type checking is very useful, a great invention. Sometimes however, it is nice to be able to side-step it if it gets in your way. C allows that.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #13 on: November 02, 2012, 02:26:50 am » |
EEPROMWriteAnything uses templates. Here: #include <Arduino.h> // for type definitions #include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value) { const byte* p = (const byte*)(const void*)&value; unsigned int i; for (i = 0; i < sizeof(value); i++) EEPROM.write(ee++, *p++); return i; }
The templated type T is used to write to EEPROM.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #14 on: November 02, 2012, 02:27:56 am » |
Type checking is very useful, a great invention. Sometimes however, it is nice to be able to side-step it if it gets in your way. C allows that.
If you buy a gun, you can shoot yourself in the foot. If you want to.
|
|
|
|
|
Logged
|
|
|
|
|
|