-
-
Notifications
You must be signed in to change notification settings - Fork 7k
WString.h: Add const qualifier to begin
and end
functions
#4945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Looks good to me! |
Actually, after the PR where I implemented this was added, I realized they should not be The functions really need to be: char* begin() { return buffer; }
char* end() { return buffer + length(); } The ranged loop shouldn't be read-only. It doesn't make sense to force users to use a standard for loop and subscript operator to be able to write elements ( Currently the code below fails, and should be working: String str = "test";
for( char &c : str ){
c = 'a';
} |
Without const, this simple sketch fails to compile: void test(const String &t)
{
for (char c : t) {// passing 'const String' as 'this' argument of 'const char* String::begin()' discards qualifiers
Serial.print(c);
}
}
void setup() {}
void loop() {
test(F("sdfsdf"));
} Do you know if it is possible to add both functions, and let the compiler select which one to use? |
Anyway, the signature of the begin function now is |
Yes, with both versions of the functions, both examples compile fine. Looks like a good compromise. char* begin() { return buffer; }
char* end() { return buffer + length(); }
const char* begin() const { return buffer; }
const char* end() const { return buffer + length(); }
The operator[] includes both methods (read/write), so this should follow suit. The problem you have mentioned is why I have this PR: #3096 Also, @matthijskooijman has mentioned in there the expectation to be able to store a null mid string. |
@Chris--A OK, tried it and it works perfectly. May I update the pull request to add the non-const functions? |
Yeah, just in-case, the non const versions cannot use |
begin() and end() only allowed read access, these changes now allow both.
I have added a PR to your branch patch-1 which adds these changes to the SAM core as well. |
Modified begin() & end() for read/write
@Chris--A Merged! |
This is looking good! Just needs accepting by the big wigs now. |
Rebased and merged. Thanks you! |
No description provided.