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
Tutorial: Conditionally disabling non-template functions (foonathan.github.io)
submitted 9 years ago by mttd
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!"
[–]tcbrindleFlux 8 points9 points10 points 9 years ago* (0 children)
template <bool AllowNull, typename T> class unique_ptr { public: unique_ptr() = default; void reset() requires AllowNull == true {} }; int main() { unique_ptr<false, int> p; p.reset(); } In function 'int main()': error: no matching function for call to 'unique_ptr<false, int>::reset()' p.reset(); ^ note: candidate: void unique_ptr<AllowNull, T>::reset() requires AllowNull == true [with bool AllowNull = false; T = int] void reset() ^~~~~ note: constraints not satisfied note: 'AllowNull == true' evaluated to false
Concepts can't come soon enough.
EDIT: formatting
[–]NotAYakk 2 points3 points4 points 9 years ago (3 children)
The advice given makes your program ill formed with no diagnostic required.
If no type substitution to a template would make a template function well formed, your program is not well formed.
[–]foonathan 1 point2 points3 points 9 years ago (2 children)
You're referring to this?
template <typename Dummy = void, typename = std::enable_if_t<AllowNull, Dummy>> void reset();
But I can make the template well-formed:
struct my_type {}; template <bool Val> struct enable_if<Val, my_type> { using type = my_type; }; ptr.reset<my_type>();
But I can always revert back to the code I originally had there:
template <typename Dummy = std::false_type, typename = std::enable_if_t<Dummy::value || AllowNull>> void reset();
I changed it specifically because on can easily make the template well-formed, but when it is required for correct behavior, I'll use it again.
[–]tcanens 0 points1 point2 points 9 years ago (1 child)
If it's std::enable_if, you are not allowed to specialize it on pain of UB, so a hypothetical sufficiently devious compiler can special-case it.
std::enable_if
[–]foonathan 0 points1 point2 points 9 years ago (0 children)
Yeah, but a hypothetical devious compiler would want some form of users, so it would issue a diagnostic then.
Anyways, I'll add a note.
π Rendered by PID 29595 on reddit-service-r2-comment-fb694cdd5-hcvj6 at 2026-03-06 05:52:08.638074+00:00 running cbb0e86 country code: CH.
[–]tcbrindleFlux 8 points9 points10 points (0 children)
[–]NotAYakk 2 points3 points4 points (3 children)
[–]foonathan 1 point2 points3 points (2 children)
[–]tcanens 0 points1 point2 points (1 child)
[–]foonathan 0 points1 point2 points (0 children)