I was embarrassed today as I was teaching my granddaughters how to program, using the Arduino IDE. I asked them to enter code similar to the following and compile the sketch.
String a = "123";
int b = "123";
int c = '1';
int d = "a";
I should have tested this out before giving this lesson, but as a retired programmer, I thought I knew what would happen. I was shocked...
No compiler errors !!!!
I haven't uploaded this code or tried to execute this code in an actual Arduino. I have written a lot of Arduino code, but I have kept it simple and not used try catch or other more sophisticated (more sophisticated than using Serial.println) error handling techniques that I would have used in my commercial environment.
Can somebody please illuminate me as to what is going on here, and how to properly deal with it?
Thank you in advance
George Clay
PS: I just tested adding the following code into the setup() function
As a retired programmer, you should have known to enable warnings.
You are right. In my defense, I would assume that these would be "Hard" errors, not warnings.... Thank You for your help.
I think that UKHeliBob's results would demonstrate why these should be "hard" errors, not simply warnings that are only displayed if you select the right compiler option.
UKHeliBob… you involved in Helicopters? I was in the Marines just after the Viet Nam war. I got to ride in a lot of helicopters, and developed a respect for their pilots. Interestingly enough the only Helicopter used by the Marines, I never rode in was the Huey... The iconic helicopter of the Viet Nam war.
/Users/john/Documents/Arduino/sketch_jul26a/sketch_jul26a.ino:2:9: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]
int b = "123";
^
/Users/john/Documents/Arduino/sketch_jul26a/sketch_jul26a.ino:4:9: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]
int d = "a";
^
The IDE turns on the 'permissive' flag to tell the compiler treat many invalid operations as low-level warnings.
-fpermissive
Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive allows some nonconforming code to compile.
The whole point of C++ is to let you do what you want. Why should any of those things be errors? I might want the ascii code in a variable. I might want the memory location pointed to. Why shouldn't I be able to do those things just because someone else isn't smart enough to figure it out?
Yea, however C++ would not allow those statements. You would get Compiler errors. You can either explicitly convert from one type to another, or you can get creative with pointers, but the compiler will not compile such code.
For example if you create a C++ program in Microsoft Visual Studio and put in a line such as
int = "1";
and try to compile it, you will get an error stating
"Cannot implicitly convert type 'string' to int.
So obviously the Arduino Compiler is not a vigorous C++ compiler, and this "hole" can create all kinds of opportunity for accidently screwing up data, and the program executing and producing garbage without any hint to the operator or programmer.
This is not a positive. Your creative outside the boundary programming should have to be explicit...not implicit.
I kind of get it now. However, in other environments, you can get a lot of warnings that you truly want to ignore. That is why they usually have such compiler options.
However, this is important enough that I would agree with you, even if it means generating pages of warnings to look through and ignore.
First time I ever saw Murkysoft held up as an example of good practise.
They are the ubiquitous 800 lb gorilla. Its been years since I have worked in the UNIX world, and I do not have easy access to a UNIX C++ compiler right now. Are you going to say that there are "Good" industry standard c++ compilers that do not give you a compile error if you just try to set values of improper type?
The 'permissive' flag was added in June 2016 with the following comment:
This avoid build errors on old libraries. It may possibly be removed in
the future after a period of transition.
Perhaps after three years the 'period of transition' is over and the flag can be removed to make these kinds of mistakes errors again. Locally you can go into hardware/arduino/avr/platform.txt and remove the flag from this line:
compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto
Delta_G:
Aha, so the compiler isn't "bad". It's just that a different option has been set.
The compiler is fine. It is doing exactly what it was told to do. Someone told it to shut up about some code errors because some library writers were bad. Maybe it's time to get the library writers to fix their libraries.
I think the -fpermissive and -Wno-error=narrowing options should be moved up into the "Compiler warnings: none" option so anyone who wanted to use badly written libraries could do so by turning off compiler warnings but everyone else would get the benefit of the compiler's messages.
I also think the default should be "All" but that's just because I've seen so many people wondering why their sketch does not work as expected when the compiler warnings would have told them exactly what they did wrong.
Thank you for your erudite comments. It is for such information, I post questions to this forum. I am a fan of Arduino, and think that it is a wonderful project. However, like many "open source" projects, it does allow some sloppiness, that you would not get from top of the line commercial products.
I guess when you embrace this, you take upon yourself a need to dive deep and know the nuances and history of the product. I have always (even when I was an integrator for a major manufacturing automation company) focused on developing the solution more than becoming an expert in nuances of the tools.