views:

2308

answers:

7

Following up on this comment from the question Writing firmware: assembly or high level?:

When compiling C++ code for the Arduino platform, can you use virtual functions, exceptions,etc. Or would you want to (have to) use a subset of C++ (as described in the comment)

Any other caveats when programming for the Arduino platform?

+2  A: 

The usability of a features is not limited by the platform but rather the compiler that you are using.

I would check your compiler documentation on what language features are supported.

Martin York
Not quite...constrained hardware has a major role here in deciding if exceptions should be supported.
SDX2000
It is not a constraint of the hardware. It is a constrainted imposed on the compiler because we think it best not to overload the hardware (the hardware could theoritically support it but we dont want to use the required resources to do so).
Martin York
Any constraints on the compiler are likely to impose further constraints on the programs being compiled. The hardware could theoretically support it but only as long as your own resource demands do not exceed a certain limit. Due to the nature of the problem this limit is quite unstable (changes with your program complexity/requirements) hence its not prudent to switch exceptions ON/OFF based on this (will cause major code churn). Evidently there exists some hardware X which cannot even support basic exception handling on its own making your "constraint on compiler" argument moot.
SDX2000
You are missing the point. This is not a hardware constraint it is a constrain imposed by the compiler. Also do you have a reference to such hardware that can not support exceptions?
Martin York
I am not trying to be difficult but I would really like to understand how this is a compiler problem. All I am saying is, is there a reason why the compiler is constrained in the first place? Is it not because the hardware can barely cope with the application logic without exceptions and adding exceptions is just going to kill it (in all probability)?
SDX2000
+1  A: 

The documentation says:

The Arduino language is based on C/C++ and supports all standard C constructs and some C++ features.

It doesn't say anything about virtual functions that I can find.

I tried a simple program and it compiles fine. I haven't tested it on Arduino hardware though.

EDIT: I also haven't tested exceptions.

Mark James
+2  A: 

Comeau computing has a C++ to C compiler which supports all C++ features. Not just virtual functions, or exceptions, but also export. You would need to call Comeau to get it to target the Arduino language, but should be not too hard.

MSalters
+1  A: 

I used g++ with this Makefile[1] and started with this code[2]. C++ waist some space. But protothreads is too cool not to use.

[1] http://electrons.psychogenic.com/articles/Makefile.tpl

[2] http://blog.brush.co.nz/2008/07/protothreads/

Flinkman
+8  A: 

The Arduino environment uses the AVR version of the GCC toolchain. The code is compiled as C++, so you can use classes. Virtual functions are possible; the vtables will be stored in the .data section and have the correct addresses. In fact, the Print base class uses virtual functions to adapt the various "print" methods to the different output types.

Exceptions are not supported because of code space reasons. The Arduino environment passes "-fno-exceptions" to the compiler command line. See the source for verification of this.

Templates are supported. For example, this no-cost stream insertion operator technique works fine using a simple template and inline operator.

Ben Combee
+3  A: 

The Arduino software uses avr-gcc to compile sketches. The following limitations were sourced from the avrlibc FAQ (Can I use C++ on the AVR?):

Supported

  • Virtual functions
  • Constructors and destructors (including global ones)

Not supported

  • C++ standard functions, classes, and template classes
  • operators new and delete (attempting to use them will cause the linker to complain about undefined external references). This means that objects can only be created on the stack. If dynamic memory allocation is required it must be implemented using malloc() and free() of C types
  • Exceptions. Since exceptions are enabled by default in the C++ frontend, they explicitly need to be turned off using -fno-exceptions in the compiler options. This is done automatically by the Arduino IDE when it launches avr-gcc

Other issues

  • Some of the supplied include files are not C++ safe, i.e. they need to be wrapped with
    extern "C" { . . . }
Matthew Murdoch
A: 

if you can create relationship with hardware example printer by c++ or c# please send code of this for my e-mail : [email protected]

omid