Go Down

Topic: Add constexpr to the refrence and favour them over #define (Read 3073 times) previous topic - next topic

PORTB

I have seen many arduino programs with many, many #define constants that should be using c++'s constexpr.

Read here: https://stackoverflow.com/questions/42388077/constexpr-vs-macros/42388687#42388687

There should be a page about how to use constexpr on the arduino refrence, and it should be preferred over #define.


Of course, using #define for pre-processor if statements (#if) is a different thing, and should be allowed.

pert

There should be a page about how to use constexpr on the arduino refrence
Feel free to submit a pull request to add one:
https://create.arduino.cc/projecthub/Arduino_Genuino/contribute-to-the-arduino-reference-af7c37

and it should be preferred over #define.
We already recommend the use of const instead of #define:
https://www.arduino.cc/reference/en/language/structure/further-syntax/define/
Quote
In general, the const keyword is preferred for defining constants and should be used instead of #define.
So now you need to convince me of why the average Arduino user would be better off using constexpr instead of const as an alternative to #define.

Keep in mind that constexpr is C++11 and the Intel Galileo and Intel Edison are using C++98. Yes, those boards are discontinued, but a lot of people own them and it's extremely unlikely their hardware packages will ever be updated. That's not to say that we can never use C++11 features, but only that there needs to be a good reason for doing so.

westfw

I have a vague desire to keep most of recommended arduino constructs within the subset of C++ that is also available in C...

pert

Previously, when I thought that there was no Arduino Language and .ino files were written in C++, my feeling was that the Arduino Language Reference only needed to document the Arduino core library API and some fundamentals of C++ to get people started. Once a user became more experienced with programming, they could simply use one of the excellent C++ references to learn the rest of the language.

Now I've been informed by Arduino that .ino files are not written in C++. They are written in the Arduino Language. The Arduino Language Reference is the only existing documentation of the Arduino Language. I feel that it's essential for a programming language to be documented 100%.

westfw


Quote
I've been informed by Arduino that .ino files are not written in C++.
If that becomes true in a technical sense, rather than just as some marketing sound-bite, it will be very sad.  :-(


Quote
The Arduino Language Reference is the only existing documentation of the Arduino Language. I feel that it's essential for a programming language to be documented 100%.
And THAT will become a huge task.

CrossRoads

I believe, according to my software engineer son, that you program in the IDE in C++ using Arduino defined Libraries, Macros, Functions, etc. that are also written in C++ when desired.

The Reference takes you to those Arduino defined things, and the  AVR Gnu compiler that is called behind the scenes
https://gcc.gnu.org/wiki/avr-gcc
will pretty much let you pull in C or C++ as you desire.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

pert

If that becomes true in a technical sense, rather than just as some marketing sound-bite, it will be very sad.  :-(
I wouldn't concern yourself about this statement indicating any plans for a technical change. It's simply a philosophical stance. Arduino's official opinion is that the sketch preprocessing done on .ino files justifies calling it a new programming language:
  • Add #include <Arduino.h> to the primary .ino file (if the file doesn't already contain that #include directive.
  • Add function prototypes for any functions in .ino files that don't already have a prototype.

After preprocessing, the sketch is indisputably C++ and is compiled using a standard C++ compiler.
Certainly the sketch preprocessing allows you to write code in .ino files that is not valid C++ (though valid C++ will always work too).

The doubts in my mind:
  • Does sketch preprocessing represent a significant enough difference to justify calling it a new language? I don't know whether there is a formal definition for what makes a new programming language or if it's merely a matter of "it's a new programming language if I say so".
  • Even if it is technically permitted to call it a new language, is doing so beneficial to the users?



And THAT will become a huge task.
I agree. Things with the documentation were so much more simple when I thought .ino files were written in C++. Several times over the years, I've responded to suggestions that more advanced documentation be added to the Language Reference by saying something like: "The Arduino Language Reference is not intended to be a complete documentation of C++. Excellent C++ references already exist elsewhere on the Internet so there's no need for us to attempt to duplicate all that content." It sure is great to be able to pass the buck!

On the other hand, the C++ references I mention (e.g. cplusplus.com) have a lot of information that is not relevant to Arduino users and may not be written in quite as beginner friendly a manner as Arduino users have come to expect. So I suppose that a custom tailored complete C++ whoops, Arduino Language reference would be helpful.

I believe, according to my software engineer son, that you program in the IDE in C++ using Arduino defined Libraries, Macros, Functions, etc. that are also written in C++ when desired.
This is what I always believed as well, but now that I work for Arduino I'm required to change my mind and do my support and documentation work based on that belief.

bobcousins

  • Add #include <Arduino.h> to the primary .ino file (if the file doesn't already contain that #include directive.
  • Add function prototypes for any functions in .ino files that don't already have a prototype.


Is  that the full extent of the "few subtle differences" that @pnndra refers to on the github thread?

This is one of those category questions which tax people but ultimately probably doesn't make much practical difference. Practically, there is some conversion required to turn a valid Arduino sketch into a valid C++ program, even if it is minimal.

This leads to conversations like:

"My Arduino sketch doesn't compile in my C++ IDE"
"Well, you need to make some changes"
"Oh, I thought Arduino was C++?"
"Well, it is, but not quite..."

Anyway, if the founders think it is a new language then whatever we say will not change what they think. All we can advise is that an Arduino language sketch is very similar to a C++ program, and outline the differences.
Please ask questions in the forum so everyone can benefit. PM me for paid work.

pert

There is one other thing I think should be added: If there are multiple .ino files in the sketch folder (shown as tabs in the IDE), concatenate them starting with the file that matches the sketch folder, followed by the rest in alphabetical order. Add the .cpp file extension to the resulting file.

That's it. It really isn't a huge difference. Now, there are a couple other things mentioned in that thread which might appear to some to be additional differences, but they're really not:

Quote
Arduino Language has its own data types and functions.
These are declared in Arduino.h so this is covered by the explanation of the preprocessor inserting the #include directive for that file. It's just a C++/C library, no magic. The "own data types" (byte, boolean, word) are just typedefs for C++ types.

Quote
necessity to "restrict" user to a init and loop function
@pnndra meant to say setup, not init. C++ programs must have a main() function but Arduino you don't need to have this function in .ino files, which might seem like a significant difference from C++. But this is not a difference at all. C++ allows multiple source files and Arduino's main() just happens to be defined in a separate file. That main() function calls setup and loop:
https://github.com/arduino/ArduinoCore-avr/blob/1.6.23/cores/arduino/main.cpp#L33-L51
Code: [Select]
int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

You could define setup() and loop() in a .cpp instead of a .ino file in the sketch if you like. So this is nothing specific to the Arduino programming language, it's just C++ being used exactly as it was intended to be used. You can define your own main() in the .ino file if you don't like the default main() so it's incorrect to say "restrict".

Anyway, if the founders think it is a new language then whatever we say will not change what they think. All we can advise is that an Arduino language sketch is very similar to a C++ program, and outline the differences.
I think that's a reasonable approach. I realized that I'm no expert in whether those three differences I mentioned are sufficient to claim a new programming language so I have no ground to argue. My interest is in making it as easy as possible for people to learn this programming language, whatever you want to call it. I've been giving a lot of thought about how that can be done. After some good discussion in another thread:
https://forum.arduino.cc/index.php?topic=620437
I submitted an issue report to the repository that contains the Arduino Language Reference content:
https://github.com/arduino/reference-en/issues/623
I'm hoping to get some input from some people at Arduino on that issue and then start improving the documentation in this respect.

jperryma

Arduino language is a greatly simplified make language. Make tools such as cmake are too much for beginners. In this sense of make, it is a language.

The ino and h files are pure C++ which allows c. Nothing gets translated or modified in the ino files.000 As mentioned before, an include is inserted for arduino.h. Serial, Serial1, Serial2 & Serial3 are C++ objects which means all ino files must be C++ even when no C++ functionality is used.

As for header files, they are not automatic and you must write #includes. Since the ino files are merged, you can do a single include before setup() and they will apply to all subsequent ino files. The Arduino language only merges ino files and adds include for arduino.h.

You can also have c and cpp files that the Arduino language will compile separate from the ino files. The drawback is you must have the appropriate includes (e.g. try using Serial.print).

Regards, Jon.

jperryma

I forgot to mention constexpr vs #define. In principle, const or constexpr is preferred. For arduino, it doesn't really matter although #define sets a bad example for noobs. The names used are global regardless of #define or const. As for constexpr vs const, is there a difference for arduino because accessing data in program storage is non-standard (see F( ) macro).

Regards, Jon. 

pert

Arduino language is a greatly simplified make language. Make tools such as cmake are too much for beginners. In this sense of make, it is a language.
I'm having trouble understanding what you mean by this, but I'm interested. Could you provide an example of some Arduino language code as it applies to your definition of the Arduino language?

Nothing gets translated or modified in the ino files.000
I think that would be a good thing to mention in my proposed documentation of the difference between Arduino programming language and C++. The sketch preprocessor adds things to the content of the .ino files, but it does not modify the existing code.

The drawback is you must have the appropriate includes
This is because sketch preprocessing is only done on .ino files. The rest are compiled just as you write them using a standard C++/C/Assembly compiler. It's absolutely certain the non-.ino files of the sketch are not written in Arduino language. For this reason, you also have to write your own function prototypes in those files.

jperryma

Sorry it's taken so long but I tried replying several times without success.

I was wrong about build generating the function prototypes being generated. Since it didn't handle moving my class headers, it was not doing the function prototypes.

The build process is clearly documented at https://github.com/arduino/Arduino/wiki/Build-Process. It merges the files, adds function prototypes for functions declared in the INO files and add the include for arduino.h.

Make is a language that programmers use to compile their programs. Arduino has changed this process so that the user only specifies the board. The make language has been reworked so that the board manufactures and arduino ide has created the required make information for the user.

This is real C++.

It is legal to call this Arduino language even though they didn't make any changes to the the language. Common examples would be Android and Ubuntu are actually Linux. In fact, there are several Linux distributions that don't state they are Linux.

I think it's a good idea that this is called Arduino language because beginners are still missing several key concepts.

Regards, Jon.

pert

#13
Jun 21, 2019, 12:33 am Last Edit: Jun 22, 2019, 02:57 am by Coding Badly
The build process is clearly documented at https://github.com/arduino/Arduino/wiki/Build-Process. It merges the files, adds function prototypes for functions declared in the INO files and add the include for arduino.h.
Yes, that's great documentation, but that information is not very accessible to someone trying to learn about the Arduino programming language.

Make is a language that programmers use to compile their programs. Arduino has changed this process so that the user only specifies the board. The make language has been reworked so that the board manufactures and arduino ide has created the required make information for the user.
I'm well aware of what the make language is. What I don't understand is what that has to do with the Arduino programming language. The only thing I can think of is that your opinion is that boards.txt, programmers.txt, and platform.txt are written in the Arduino programming language, rather than the code in the .ino files. Is that what you're saying?

I don't think it's accurate to say "the make language has been reworked", since Arduino never started with the make language to begin with. Sure they have a build system, but it's not based on make.

I think it's a good idea that this is called Arduino language because beginners are still missing several key concepts.
I don't understand what you mean by that. Please explain further.

jperryma

Quote
great documentation, but that information is not very accessible to someone trying to learn about the Arduino programming language.
My perspective is that Arduino should remain simple so beginners won't get overwhelmed. As more advanced discussions occur, web searches will be difficult to determine the relevance.

Quote
I'm well aware of what the make language is. What I don't understand is what that has to do with the Arduino programming language.
#include arduino.h is standard C. Arduino programmers can easily combine files and create function prototypes. The only uniqueness comes with the build process. We were discussing what is the uniqueness of the Arduino language. Maybe the only justification is to search for Arduino versus C++.

Quote
I think it's a good idea that this is called Arduino language
For beginners, searching on "Arduino" returns far more useful results than C or C++.

Regards, Jon.

Go Up