What is the significance of the underscore _ ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <vector>

class AddressBook
{
    public:
    // using a template allows us to ignore the differences between functors, function pointers 
    // and lambda
    template<typename Func>
    std::vector<std::string> findMatchingAddresses (Func func)
    { 
        std::vector<std::string> results;
        for ( auto itr = _addresses.begin(), end = _addresses.end(); itr != end; ++itr )
        {
            // call the function passed into findMatchingAddresses and see if it matches
            if ( func( *itr ) )
            {
                results.push_back( *itr );
            }
        }
        return results;
    }

    private:
    std::vector<std::string> _addresses;
};


What is the significance of the underscore preceding addresses?
Nothing. It's just how whoever wrote this function decided to name their variable. They probably have some convention (i.e. maybe they want all class variables to begin with underscores versus local variables having none) for naming their stuff, but you'd have to ask them.
It seems a naming convention.
But it is wrong, because in C++ you should avoid using variable names starting with underscore, as it is reserved for the C++ implementers.
fcantoro wrote:
it is wrong, because in C++ you should avoid using variable names starting with underscore, as it is reserved for the C++ implementers.

There is nothing wrong with the name. Names starting with an underscore are only reserved if they are in the global namespace, or if they start with two underscores or one underscore followed by an upper case letter.
closed account (z05DSL3A)
Peter87 wrote:
Names starting with an underscore are only reserved if they are in the global namespace, or if they start with two underscores or one underscore followed by an upper case letter.
[sarc]...or if the wind blows from the east, or a black cat crosses your path.[/sarc]

I've always been told it is best to avoid starting a name with an underscore.
...yet the standard is pretty specific. OP's use of underscore (in a class) is only dangerous if he decides to capitalize the first letter.

Personally, I like it. It's very clean and easy to read, IMHO.

(Unlike those stupid 'm_'s.)
Last edited on
@Duoas can you please say what's wrong with 'm_'? Using a letter before the underscore allows you to distinguish more than just member variables. It allows you to create a key where just looking at something's name will simply tell you its scope or purpose and stops the need to change the names of things further to stop clashing. For example, I personally use m_ for private member variables and M_ for private member functions so that they are clearly distinguished, whereas putting a single _ before each could lead to confusion and not distinguishing between private and public member functions in the definition code and documentation can also lead to confusion.
@shadowmouse: it's his personal opinion. Personally I don't like to change variable names to include information my IDE can tell me.
Last edited on
I realise that. It's just that many personal opinions are based on something more concrete, such as the fact that people could get carried away with letters before underscores which would lead to people discouraging it. I wasn't trying to start an argument. I'm curious.
Last edited on
That's fine but I put single underscore only when it's private member that has public reference with the same name.
My personal opinion is that private members don't have to be distinguished from public like m_ or M_, as it's worse to read. There can be special cases when it's better, but not in common.
Last edited on
@shadowmouse
Nothing particularly concrete, just years of looking at code and deciding what I think is clean and readable.

I prefer a very clean style. It is often very useful to distinguish non-public member objects from public member objects, but I have never liked the 'm_' because it just makes code look messier (in my opinion). The underscore reads very much like whitespace, so I don't keep trying to decode it in the corner of my brain like happens with the 'm_'.
Topic archived. No new replies allowed.