1

I'm abstracting the interrupt handling on multiple microcontrollers. The ARM Cortex M-3/4 ones do greatly support the STL, but the ATMega328p (which are greatly used) do not support the C++ STL. I'd like to still offer the possibility to register callbacks for specific interrupts utilizing the std::function additionally to the basic C-style function pointer. I haven't tested it yet, but was told that the C++ lambdas can do a lot of stuff compared to the simple function pointers, especially when it comes to capturing variables and references.

I'd like to change the defining header class(if necessary) and the implementing classes(if necessary, might not be necessary, because the implementation will simply be missing) in order to remove the member function definition at compile time depending on whether the controller supports or doesn't support the STL. I was wondering if C++ offers such a test or if it's somehow possible to do this by including an STL header and somehow test, whether the include failed or not?

...

#include <functional>

class InterruptVectorTable
{
    bool setCallback(ValueType InterruptNumber, void (*Callback)(void));
    bool setCallback(ValueType InterruptNumber, std::functional<void(void)> Callback);
}

If both definitions are not allowed at the same time, then I'd like to preferably use the std::function.

walnut
  • 21,629
  • 4
  • 23
  • 59
John Behm
  • 101
  • 1
  • 5
  • Which compiler do you use? – Evg Nov 12 '19 at 12:57
  • https://stackoverflow.com/questions/5205491/whats-the-difference-between-stl-and-c-standard-library – Shawn Nov 12 '19 at 12:58
  • I am not completely clear as to what you are asking: `STL` is either an external library or an outdated term used to refer to C++ Standard Library (namespace `std`). Support for The C++ Standard Library is by the standard versions (C++11, C++14 etc) and usually controlled by compiler switches. – Richard Critten Nov 12 '19 at 12:59
  • 1
    Those are different compilers, I guess, I'm using th ePlatformIO "IDE", which a little bit hides the actual compilers. I think the ARM Cortex Compiler is the arm-gcc and the ATmega compiler is the avr-gcc one. I am referring to the usage of the C++ Standard Library, that contains the STL. And would like to test at compile time whether it is possible to include headers from that library at compile time and change the definitions accordingly if it's possible to include the headers or not. @Richard Critten https://www.microchip.com/webdoc/AVRLibcReferenceManual/FAQ_1faq_cplusplus.html – John Behm Nov 12 '19 at 12:59
  • 2
    Generally the compiler/library *should* define the [feature test macros](https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros) and since C++17 there is [`__has_include`](https://en.cppreference.com/w/cpp/preprocessor/include) which however doesn't guarantee that there is anything in the header file. Whether they are usable in this environment, I don't know. – walnut Nov 12 '19 at 13:10
  • 1
    Support for C++ features is a property of a compiler, not a microcontroller. ATMega328p executes machine instructions, not C++ code. – Evg Nov 12 '19 at 13:14
  • One way to check is using `__cplusplus` macro definition, if it is smaller than `199711L` you can assume you probably cannot use STL (which was introduced in 1993 to the C++ committee). For example, neither my 1990 SAS/C++ compiler nor my 1987 Lattice C++ have STL. – Eljay Nov 12 '19 at 13:36

1 Answers1

5

The new C++17 standard has the __has_include functionality you are looking for. However, it is a bit of a chicken and the egg problem. If your platform does not support STLs then it is likely to be to out of date for c++17.

Instead you likely want to rely on a your build system. For instance, cmake can detect which C++ standard a compiler honors, or even which part of a standard is implemented.

In your case, it might be simpler to have the build define an ad-hoc pre-processor constant HAS_STD_CPP if the standard lib is available on that platform, and then use the c preprocessor to include the STL bits or not, as illustrated in https://en.wikipedia.org/wiki/C_preprocessor.

mdavezac
  • 515
  • 2
  • 7