anilgkts / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

cast away constness #866

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A user on the Arduino forum encountered a bug when using avr-gcc version 4.5.1. 
 The bug is the attempt to cast away the const qualifier with reinterpret_cast. 
 Section 5.2.10.2 of the C++98 spec states in part the following:

"The reinterpret_cast operator shall not cast away constness."

A possible fix is shown below.

Line 38 in WString.h
From:
#define F(string_literal) (reinterpret_cast<__FlashStringHelper 
*>(PSTR(string_literal)))

To:
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper 
*>(PSTR(string_literal)))

The forum thread
http://arduino.cc/forum/index.php/topic,91314.msg735641.html#msg735641

Original issue reported on code.google.com by mkwi...@gmail.com on 23 Mar 2012 at 5:40

GoogleCodeExporter commented 9 years ago
The const qualifier is required or compilation will fail with

In file included from 
/opt/cross/avr/lib64/gcc/avr/4.6.3/../../../../avr/include/inttypes.h:37:0,
                 from /opt/cross/avr/lib64/gcc/avr/4.6.3/../../../../avr/include/avr/pgmspace.h:86,
                 from /usr/local/pckg/arduino/arduino-1.0.1/hardware/arduino/cores/arduino/Arduino.h:8,
                 from output/Seismograph.cpp:3:
/opt/cross/avr/lib64/gcc/avr/4.6.3/include/stdint.h:3:3: warning: #include_next 
is a GCC extension [enabled by default]
In file included from output/Seismograph.cpp:135:0:
/home/volker/sketchbook/libraries/Streaming/Streaming.h:84:15: warning: unused 
parameter 'arg' [-Wunused-parameter]
output/Seismograph.cpp: In function 'void printInfo()':
output/Seismograph.cpp:473:8: error: reinterpret_cast from type 'const char*' 
to type '__FlashStringHelper*' casts away qualifiers

Interestingly, the fix is in arduino IDE 1.0, but missing in 1.0.1.
The correct version is

#define F(string_literal) (reinterpret_cast<const __FlashStringHelper 
*>(PSTR(string_literal)))

Original comment by gooc...@top.geek.nz on 29 Jul 2012 at 5:41

GoogleCodeExporter commented 9 years ago
Patch attached

Original comment by gooc...@top.geek.nz on 29 Jul 2012 at 5:50

Attachments:

GoogleCodeExporter commented 9 years ago
Sorry, it's missing in IDE 1.0 as well - it was me who added it there some time 
ago... *vbg*

Original comment by gooc...@top.geek.nz on 29 Jul 2012 at 6:31

GoogleCodeExporter commented 9 years ago
https://github.com/arduino/Arduino/commit/bc79a998e99d229c4add23e8075995119d5454
85

Original comment by dmel...@gmail.com on 21 Aug 2012 at 2:14