Skip to content

Conversation

Chris--A
Copy link
Contributor

This will allow using the String library in a ranged for loop. Also the additions expose the first and last character pointers, which may be of use somewhere.

void setup() {

  Serial.begin( 9600 );
  String s = "Hi, this is a test of C++11 iterators.";

  for( char c : s ){
    Serial.print( "Character in string: " );
    Serial.println( c );
  }
}
void loop(){}

This modification is placed inside the check for C++11 which is also due for update here: matthijskooijman@6624d61

This will allow using the String library in a ranged for loop:

```C++
String s = "Hi, this is a test";

for( char c : s )
  Serial.print( c );
```
@Chris--A
Copy link
Contributor Author

I wanted to have the begin and end function return types based of the c_str function, however it seems not possible due to String being incomplete at this point in code.

auto end() -> decltype( ((String*)nullptr)->c_str() ) { return c_str() + length(); }

However I left it in to clearly show that it is a C++11 feature ( new declaration syntax ). I chose this because many Arduino libs have begin() as a method to delay construction and avoid the static initialization order problems, however this begin has quite a different meaning.

What do you think is best? maybe a comment above the lines showing that they are intended for ranged loops, and not construction/initialization of something.

As I was not able to base the return types of `begin()` & `end()` off the c_str() function, I have changed the source so the features can be used by C++98 code, while still allowing ranged loops in C++11.
@Chris--A
Copy link
Contributor Author

As C++98 code does use begin and end for stl iterators, and because the dependency for c_str() is not possible, I have re-written them into functions usable by both 98 and 11 standards.

@matthijskooijman
Copy link
Collaborator

Sounds good.

@cmaglie cmaglie added this to the Release 1.5.8 milestone Jul 15, 2014
@matthijskooijman matthijskooijman added Component: Core Related to the code for the standard Arduino API Version: 1.5.x feature request A request to make an enhancement (not a bug fix) labels Sep 11, 2014
@cmaglie cmaglie modified the milestones: Release 1.5.8, Release 1.5.9 Oct 13, 2014
@cmaglie cmaglie modified the milestones: Release 1.6.0, Release 1.6.1 Feb 18, 2015
@Chris--A
Copy link
Contributor Author

This update does not need to wait until C++11 is turned on.

It can be of use now, just like STL containers:

for( char *ptr = s.begin() ; ptr != s.end() ; *ptr++ = /* modify each character in string */ );

It might be worth noting if documented, that end() returns the null pointer, so the loop above and a C++11 ranged loop do not include the null in the iteration.

@Chris--A
Copy link
Contributor Author

Now that C++11 has been enabled, can this be merged too!

@ffissore ffissore modified the milestones: Release 1.6.6, 1.6.7 Nov 4, 2015
@ffissore ffissore modified the milestone: Release 1.6.7 Dec 14, 2015
cmaglie added a commit that referenced this pull request Apr 20, 2016
@cmaglie cmaglie added this to the Release 1.6.9 milestone Apr 20, 2016
@cmaglie
Copy link
Member

cmaglie commented Apr 20, 2016

Rebased and merged, thanks @Chris--A !

@cmaglie cmaglie closed this Apr 20, 2016
ollie1400 pushed a commit to ollie1400/Arduino that referenced this pull request May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants