I have been studying a bit of C++ recently to improve a bit my knowledge...
Now I have stumbled on a couple of coding lines that I struggle to understand ...
they are from the fastLED library:
I paste them below:
The library information explains what they are for and how to use them, but I am trying to understand the syntax.
The first one is just a statement in capital letter in the code without any #define in front of it, how would the compiler know what to do with it (it's not either using the "using" keyword in the normal way ).
The second one is like a creation of an object? but uses a syntax that I don't know (with angle brackets, arguments, and with the addition of function at the end of it....?
I'd be grateful if you can please point me to a place where I can get a bit more familiar with these things please.
That's simply a macro defined elsewhere. Most platforms seem to define it as nothing, but STM32 has:
#define FASTLED_USING_NAMESPACE using namespace NSFastLED;
(I don't know offhand WHY STM32 needs that, and other platforms don't.)
This sort of macro is pretty common when platform dependencies cause certain code to be needed only some of the time. An example common in the Arduino environment is "PROGMEM", which is needed (and DOES something) on AVR because flash (PROGram MEMory) and RAM have different address spaces, but is unnecessary for ARM and other architectures with a single address space.
The angle brackets are a thing that goes with "templates", a C++ feature that allows a single function (source code) definition to operate on multiple data types. That one looks a bit too complex for me to explain further, due my own limited C++ experience. It looks like a "simple" function call to a particular instance of "setCorrection" that is applicable to a particular type of LED strip.
One of the things I don't like about C++ is that it seems to be really common for "general purpose" libraries to descend into this sort of "template hell" that makes them near impossible for people with "intermediate expertise" to understand.
For example, I've been baffled every time I try to look at the source for the STL
void setup() {
#ifdef FASTLED_USING_NAMESPACE
#warning "using FASTLED_USING_NAMESPACE"
#else
#warning "not using FASTLED_USING_NAMESPACE"
#endif
}
void loop()
{
// put your main code here, to run repeatedly:
}
Compiler output
/tmp/arduino_modified_sketch_4451/sketch_jun27a.ino:6:2: warning: #warning "not using FASTLED_USING_NAMESPACE" [-Wcpp]
#warning "not using FASTLED_USING_NAMESPACE"
^~~~~~~
#include <FastLED.h>
void setup()
{
#ifdef FASTLED_USING_NAMESPACE
#warning "using FASTLED_USING_NAMESPACE"
#else
#warning "not using FASTLED_USING_NAMESPACE"
#endif
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
}
Thanks, in fact I realise that I have used it myself before... to conditionally compile debug messages (i.e #ifdef DEBUG
Serial.print....etc etc #endif
I think this case confused me as there is just that statement after the inclusion of the library and no more mention of it in the code...