Author Topic: wxString const& or const wxString&  (Read 10924 times)

Offline Martin K.

  • Multiple posting newcomer
  • *
  • Posts: 86
wxString const& or const wxString&
« on: November 05, 2012, 02:06:56 pm »
Hi,

I know that this is not really a Code::Blocks related question, but maybe it is some type of Code::Blocks coding standard:

Given is a function that accepts a reference to a wxString. This function will never change this string. What is your prefered const handling?

function(const wxString& str)
{
}

or

function(wxString const& str)
{
}


I can found the first and the second form in Code::Blocks source, as far as i can see the second form in newer code.
GCC seems to handle all forms as the same, a str += _T("test") will fail.
In my opinion the 2. form should be the right form (reference to constant wxString), what do you mean about this?

Martin

P.S. GCC don't like (const wxString const & str)
« Last Edit: November 05, 2012, 02:09:41 pm by Martin K. »

Offline carra

  • Multiple posting newcomer
  • *
  • Posts: 117
Re: wxString const& or const wxString&
« Reply #1 on: November 05, 2012, 02:17:54 pm »
They don't mean the same:

const wxString& str
This is a reference to a constant string

wxString const& str
This is a constant reference to a string. The string can still be changed, just not the reference to it.

In general you will want the first alternative.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: wxString const& or const wxString&
« Reply #2 on: November 05, 2012, 02:23:27 pm »
A const &a and const A &a are the same.
In C::B the standard form is the latter - const A &a.
If you see the former, probably it is added by me because I like it more and I've forgotten to obey to the standard.

See here for the coding style guide: http://wiki.codeblocks.org/index.php?title=Coding_style

@carra: You are in deep misunderstanding. wxstring & const str is const reference to the string, but this is invalid C++ see here: http://www.parashift.com/c++-faq/const-ref-nonsense.html
Some const correctness FAQs can be found here: http://www.parashift.com/c++-faq/const-correctness.html

This same question discussed in stackoverflow: http://stackoverflow.com/questions/3694630/c-const-reference-before-vs-after-type-specifier
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Martin K.

  • Multiple posting newcomer
  • *
  • Posts: 86
Re: wxString const& or const wxString&
« Reply #3 on: November 05, 2012, 02:32:09 pm »
again what learned  ;)

further investigations tell me that a reference itself is const by design, it can't be changed. So "wxString const&" is redundant. "const wxString&" is the right way, although gcc seems to handel them equally.

Thank you.

Offline carra

  • Multiple posting newcomer
  • *
  • Posts: 117
Re: wxString const& or const wxString&
« Reply #4 on: November 05, 2012, 02:59:19 pm »
You were actually right, obfuscated. What I said had no sense because in fact, once created, C++ references cannot be repointed to another variable! If you try to change it, it performs assignment on the pointed variable instead.

I just wrote a small program to show the differences.
This should give compile errors in the last 2 appendings:

Code
#include <iostream>     // [ C++ STL ] I/O Streams
#include <string>       // [ C++ STL ] Strings

using namespace std;

int main()
{
    string str = "string";
    string another;
   
    string& const StringRefConst = str;
    string const& StringConstRef = str;
    const string& ConstStringRef = str;
   
    // attempts to change the string
    StringRefConst += " changed from StringRefConst";   // allowed
    StringConstRef += " changed from StringConstRef";   // error
    ConstStringRef += " changed from ConstStringRef";   // error
   
    return 0;
}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: wxString const& or const wxString&
« Reply #5 on: November 05, 2012, 04:35:40 pm »
So "wxString const&" is redundant. "const wxString&" is the right way, although gcc seems to handel them equally.
No, you're still getting it wrong. Both are the same and it is up to a personal/project preference.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline carra

  • Multiple posting newcomer
  • *
  • Posts: 117
Re: wxString const& or const wxString&
« Reply #6 on: November 05, 2012, 05:31:11 pm »
Both are the same
And this is why const wxString const & str is redundant. Therefore the compiler won't accept it.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: wxString const& or const wxString&
« Reply #7 on: November 05, 2012, 05:44:40 pm »
And this is why const wxString const & str is redundant. Therefore the compiler won't accept it.
No, this is the same as const const wxString, so it is just a syntax error...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]