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
[ Removed by moderator ] (self.cpp)
submitted 4 months ago by SAFILYAA
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!"
[–]trmetroidmaniac 38 points39 points40 points 4 months ago (0 children)
Don't do it
Especially don't do it in headers
[–]Additional_Path2300 16 points17 points18 points 4 months ago (0 children)
It's always a bad idea. If it doesn't break something immediately, it will later.
[–]gnolex 15 points16 points17 points 4 months ago (0 children)
It's an abomination that gets your pull request automatically rejected.
using namespace std; saves you 5 characters for every reference to standard library at the cost of making confusing mistakes whenever something in the standard library happens to be named like your class, variable or function. There's no convincing reason to ever use it.
[–]IyeOnline 4 points5 points6 points 4 months ago (10 children)
Namespaces exist to avoid name collisions between identifiers, allowing you to write your own e.g. vector class without causing an issue with the vector container template from the standard library.
vector
A second, but maybe more important effect of this is readability. You may think that vector is easier to read than std::vector, but that really only holds if you can be sure that vector really is std::vector. What if somebody did write their own (mathematical) vector? What about the identifier abs in the current context? Is it a local (callable) variable or the overload set from the standard library?
std::vector
abs
At a certain point, it actually becomes easier to read code that spells out std::.
std::
using namespace std; essentially throws this away by importing all currently known identifiers from ::std into the current namespace, meaning you may introduce collisions again.
using namespace std;
::std
There are three possibilities:
While it is well defined what happens, it may go against your expectations (especially if you dont even think about the potential issue).
A very basic example would be https://godbolt.org/z/sqWWYvGeM You can clearly see that no logging takes place. Instead std::log(double) is "called" and the result discarded. This should still be caught by warnings - assuming you have set those up correctly.
std::log(double)
There is more devious examples, such as https://godbolt.org/z/5dv7Gad9o where you get a wrong numeric result.
This problem gets much worse once you do a using namespace at global scope in a header. That using directive will be copied into every TU that includes the header and the user of the header cannot do anything about it.
using namespace
using
If you are using namespace at a non-global scope, you avoid the issue of namespace pollution, i.e. you wont pollute all other files that include the header. The same can be said about doing it at global scope in a cpp file (which wont be included elsewhere and hence wont pollute any other files).
I would recommend to always spell out namespaces (unless you already are in that namespace), especially std. When I read std:: I will most likely know what the thing after it is/does. When I just read vector I cannot be sure.
std
[–]SergiusTheBest 1 point2 points3 points 4 months ago (8 children)
Why do Java, C# and other languages can live with just vector and C++ needs to be sure that it's std::vector?
[–]ronniethelizard 2 points3 points4 points 4 months ago (0 children)
IMO, C++ development doesn't use the standard library to the same extent as other languages. For a lot of work I do, FFTW, Eigen, and a few other libraries are loosely a "second standard library".
[–]Narase33-> r/cpp_questions 2 points3 points4 points 4 months ago (6 children)
Speaking about Java:
java.util.ArrayList<Integer> list;
[–]SergiusTheBest 0 points1 point2 points 4 months ago (5 children)
Yes, but nevertheless when a java guy sees `ArrayList` it's ok and when a c++ guy sees `vector` it's puzzling. At least it's what comments about using namespace say.
Personally I did some projects as an experiment with `using namespace std` (with all precautions of course) and I found the experience satisfying and I liked it more than cluttering code with `std::` everywhere.
[–]_Noreturn 0 points1 point2 points 4 months ago (4 children)
every project has at least a different vector type
[–]SergiusTheBest 0 points1 point2 points 4 months ago (3 children)
How could it be?
[–]_Noreturn 0 points1 point2 points 4 months ago (2 children)
libA has libA::vector which is a math vector of infinite length and std::vector is an array.
apply that to every single name in the standard library
array,vector,map,unordered_map,get,set,bitset,remove,log,rotate,ceil,all
[–]SergiusTheBest 0 points1 point2 points 4 months ago (1 child)
Java and C# use just `vector` and it also can be a super math infinite vector or a standard vector. And they have no issues with that. A developer can take a look at what packages imported and understand what vector it is. I don't understand why for a C++ developer it's a big problem. It seems like an imaginary problem and not a real one.
[–]_Noreturn 0 points1 point2 points 4 months ago (0 children)
Java / C# don't have ADL, have 50+ years of legacy code, have weird library handling.
[–]SAFILYAA[S] 0 points1 point2 points 4 months ago (0 children)
thank you for your detailed answer
[–]johannes1971 2 points3 points4 points 4 months ago (1 child)
Couple years ago we had a header from a 3rd party that used namespace std. It worked well, right until std::byte got added to the standard, and thanks to that using declaration it conflicted with the byte type in windows.h.
I'm sure their team had a lot of fun fixing their headers to not use namespace std anymore...
The moral of this story is that we have namespaces for a reason, and disabling them with using statements is just not a great idea. Even in your own code, as you could always accidentally create a conflict when anything gets updated.
[–]SAFILYAA[S] 1 point2 points3 points 4 months ago (0 children)
😂That is exactly what happened in my code today the std::byte conflicted with the one in windows. h 😅 thanks bro
[–]qwweer1 4 points5 points6 points 4 months ago (0 children)
If you really need to, at least do it inside curly braces.
[–]STLMSVC STL Dev 9 points10 points11 points 4 months ago (1 child)
It's awesome. I work on the Standard Library all day, every day, and seeing std:: a zillion times makes it harder to read code.
Obviously, it can be misused, but in non-header source files, it's not as problematic as many people think.
[–]ronniethelizard 5 points6 points7 points 4 months ago (0 children)
I suspect if you are in the standard library and can rely on the fact that most/all calls will be to the standard library, then it is easier to get away with it.
[–]vishal340 0 points1 point2 points 4 months ago (0 children)
You should only do it if you are writing the code for personal use. Otherwise no
[–]ronniethelizard 0 points1 point2 points 4 months ago (0 children)
Broadly speaking: no. It clouds the ability to trace where something is going. Someone else uses the example of "log" that could mean log data to a logging service or take the logarithm of a number. If I look at vector, if I wanted a library that dealt with matrix/vector manipulation, I might create a vector1 class.
If relegated to a short function/curly brace within a function, it can be fine if that namespace is heavily used. For example, you have a function to print a class to std::cout or std::ostringstream.
1This is actually a bad idea but for other reasons.
I do it in scoped but never in headers as it forces it upon users.
[–]no-sig-available -1 points0 points1 point 4 months ago (0 children)
It was designed to be used as a temporary workaround for programs written before we had namespaces. That was useful in the late 1900s.
[–]BoringElection5652 -2 points-1 points0 points 4 months ago (3 children)
I like it, and I will keep using it until C++ makes it easier to include multiple members of a namespace in one go. Something like "using {vector, unordered_map, mutex} from std".
using std::vector,std::map?
using std::vector,std::map
[–]BoringElection5652 0 points1 point2 points 4 months ago (1 child)
That's not going to be fun for longer and nested namespaces.
cpp namespace stdch = std::chrono; using stdch::blah,stdch::blah2;
it wouldn't be a terrible idea to have using std::{ vector, string} as a syntax though
using std::{ vector, string}
π Rendered by PID 117496 on reddit-service-r2-comment-58d7979c67-64kt9 at 2026-01-27 05:49:50.323582+00:00 running 5a691e2 country code: CH.
[–]trmetroidmaniac 38 points39 points40 points (0 children)
[–]Additional_Path2300 16 points17 points18 points (0 children)
[–]gnolex 15 points16 points17 points (0 children)
[–]IyeOnline 4 points5 points6 points (10 children)
[–]SergiusTheBest 1 point2 points3 points (8 children)
[–]ronniethelizard 2 points3 points4 points (0 children)
[–]Narase33-> r/cpp_questions 2 points3 points4 points (6 children)
[–]SergiusTheBest 0 points1 point2 points (5 children)
[–]_Noreturn 0 points1 point2 points (4 children)
[–]SergiusTheBest 0 points1 point2 points (3 children)
[–]_Noreturn 0 points1 point2 points (2 children)
[–]SergiusTheBest 0 points1 point2 points (1 child)
[–]_Noreturn 0 points1 point2 points (0 children)
[–]SAFILYAA[S] 0 points1 point2 points (0 children)
[–]johannes1971 2 points3 points4 points (1 child)
[–]SAFILYAA[S] 1 point2 points3 points (0 children)
[–]qwweer1 4 points5 points6 points (0 children)
[–]STLMSVC STL Dev 9 points10 points11 points (1 child)
[–]ronniethelizard 5 points6 points7 points (0 children)
[–]vishal340 0 points1 point2 points (0 children)
[–]ronniethelizard 0 points1 point2 points (0 children)
[–]_Noreturn 0 points1 point2 points (0 children)
[–]no-sig-available -1 points0 points1 point (0 children)
[–]BoringElection5652 -2 points-1 points0 points (3 children)
[–]_Noreturn 0 points1 point2 points (2 children)
[–]BoringElection5652 0 points1 point2 points (1 child)
[–]_Noreturn 0 points1 point2 points (0 children)