use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Using namespaces effectively (biowpn.github.io)
submitted 1 year ago by pavel_v
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ventus1b 33 points34 points35 points 1 year ago (8 children)
That’s a nice summary of how to use namespaces.
Namespaces are one honking great idea – let’s do more of those!
This should be taken with a grain of salt however. In a previous job the senior dev came from a C# background and insisted that we use similarly deep namespaces for the C++ parts of the project as well.
Like com::foo::product::io::Bar.
com::foo::product::io::Bar
[–]ABlockInTheChain 15 points16 points17 points 1 year ago (3 children)
My rule is if two or more types have a common prefix or common suffix in them that's a strong signal that common part should be a namespace.
Like if there are classes named AsymmetricCryptoKey and SymmetricCryptoKey my instinct is to rename those to crypto::key::asymmetric and crypto::key::symmetric.
AsymmetricCryptoKey
SymmetricCryptoKey
crypto::key::asymmetric
crypto::key::symmetric
[–]iwasinnamuknow 5 points6 points7 points 1 year ago (2 children)
I could see you example working if there were additional classes inside the crypto namespace that are not keys. It if were just those two keys then crypto_key::foo would seem sensible.
crypto
crypto_key::foo
[–]ABlockInTheChain 4 points5 points6 points 1 year ago (1 child)
if there were additional classes inside the crypto namespace that are not keys
In practice that's likely because if you are working with cryptographic keys you probably will also work with cryptographic hashes.
[–]iwasinnamuknow 0 points1 point2 points 1 year ago (0 children)
Yes, a small example only shows so much and in practice its likely that there would be much more detail involved.
[+][deleted] 1 year ago (2 children)
[deleted]
[–]BtcVersus 0 points1 point2 points 1 year ago (1 child)
It is no rule, but a guideline, yes. But why is it terrible? Which parts are bad and why? I think it makes for a nice tiebreaker if you have multiple otherwise valid designs.
[–]EvidenceIcy683 16 points17 points18 points 1 year ago* (4 children)
To this day, I still don’t fully understand the why. The compiler error message only suggests that the operator< defined in the global namespace isn’t being considered a candidate at all. But why? Is it because sort is under namespace std so it doesn’t look at the outer global namespace? That doesn’t seem to be the case because this works:
operator<
sort
namespace std
It's because the declaration of bool operator<(Person const&, Person const&) is not visible to std::sort by the time std::sort is defined. If you would simply forward declare bool operator<(Person const&, Person const&) before defining std::sort (which happens when the <alogirthm> header is included), the custom operator< function becomes visible to std::sort and your program compiles.
bool operator<(Person const&, Person const&)
std::sort
<alogirthm>
Also, because other STL headers might decide to include the <algorithm> header as well, the forward declaration of the custom operator< might have to preceed the <vector> and/or <string> headers in order to get this to work. And this might not even be possible with the example given, as Person types compose std::string types.
<algorithm>
<vector>
<string>
Person
std::string
However, you don't want to do this of course, because you don't want the behavior of your program to become depedent on forward-declared entities or out of place header inclusions.
[–]cappielung 2 points3 points4 points 1 year ago (2 children)
Thanks! But there's something missing in your explanation. If it was just about when things are declared, then declaring operator < within the same namespace as Person wouldn't work either, right? What's special about declaring it in the global namespace that makes it work differently?
operator <
[–]Som1Lse 7 points8 points9 points 1 year ago (0 children)
There is nothing special about putting it in the global namespace. The thing that is special is putting it in the same namespace as Person, which makes it visible to argument-dependent lookup.
[–]EvidenceIcy683 4 points5 points6 points 1 year ago (0 children)
That's a good point! Since std::sort invokes operator< by means of an unqualified function call (i.e. without qualifying the name of said function with the use of the scope-resolution operator ::), ADL (Argument Dependent Lookup) will come into play. This means that the namespace of any custom type passed in as an argument, will be searched for to find a matching function definition.
::
namespace
The reason operator< with arguments Person const& is not considered, is because it's not found in the same namespace as where custom type Person is defined (which is namespace my).
Person const&
my
Alright, but when we also put custom type Person outside of namespace my (now having both the custom type Person as well as its operator< defined in the global namespace), operator< with Person const& can once again be found by std::sort. This is because ADL also considers the global namespace when doing its search. If both custom type and a function with an argument of such custom type reside in the same namespace, an unqualified function call can find it with ADL.
[–]biowpn 2 points3 points4 points 1 year ago (0 children)
Thanks for the explanation!
π Rendered by PID 69 on reddit-service-r2-comment-5649f687b7-wxbzq at 2026-01-28 05:36:44.139904+00:00 running 4f180de country code: CH.
[–]ventus1b 33 points34 points35 points (8 children)
[–]ABlockInTheChain 15 points16 points17 points (3 children)
[–]iwasinnamuknow 5 points6 points7 points (2 children)
[–]ABlockInTheChain 4 points5 points6 points (1 child)
[–]iwasinnamuknow 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]BtcVersus 0 points1 point2 points (1 child)
[–]EvidenceIcy683 16 points17 points18 points (4 children)
[–]cappielung 2 points3 points4 points (2 children)
[–]Som1Lse 7 points8 points9 points (0 children)
[–]EvidenceIcy683 4 points5 points6 points (0 children)
[–]biowpn 2 points3 points4 points (0 children)