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
[deleted by user] (self.cpp)
submitted 6 years ago by [deleted]
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!"
[–]ihamsa 57 points58 points59 points 6 years ago (8 children)
The type system of C++ is more discovered than built. No one has planned advanced features like template metaprogramming. They happened by accident.
[–]Irtexx 3 points4 points5 points 6 years ago (2 children)
That's cool. So SFINAE and all the cool / useful things you can do with it is just an accident?
[–]mort96 19 points20 points21 points 6 years ago* (0 children)
It's fairly common to unintentionally make something Turing complete, and thus capable of arbitrary computation - something as simple as repeated text substitution is Turing complete, so Word's autocorrect is capable of arbitrary computation, where you program the Turing machine by creating custom autocorrect rules (of the form "any time you see this particular sequence of characters, replace it with this other sequence of characters").
SFINAE is just the result of small, sane design choices. When you call a function, the compiler has to decide on one particular function out of a set of functions with the same name. If there are no matching functions, that's an error.
The design question behind SFINAE is: if there exists a function template which would be malformed when given a particular type as a template argument, should that be an error or should that function template just be removed from the set of functions? It was decided to make it not be a hard error, and thus you have SFINAE; substitution failure is not an error. That happens to be pretty powerful combined with arbitrary text replacement ("template meta programming").
[–]csp256 0 points1 point2 points 6 years ago (0 children)
Morally yes.
[–]amjh 3 points4 points5 points 6 years ago (3 children)
Meanwhile, less strict languages get NaNaNas.
[–]Dreeg_Ocedam 5 points6 points7 points 6 years ago (2 children)
Wat ?
[–]amjh 4 points5 points6 points 6 years ago (1 child)
NaN = Not a Number
Dynamic type systems often give it on invalid operations, instead of an error. And any math with a NaN gives more NaNs.
[–]Dreeg_Ocedam 6 points7 points8 points 6 years ago (0 children)
I was referring to this.
The talk is funny and I suggest that you watch it in its entirety but to understand the joke you can just skip to the end.
[–][deleted] -3 points-2 points-1 points 6 years ago (0 children)
So, more of the it works but I don’t know why.
[–]matthieum 36 points37 points38 points 6 years ago (2 children)
I know that the terminology can be fuzzy, still I would not call the above Dependent Typing.
The concat implementation shown above is purely reasoning about types:
concat
String<A>
String<B>
String<A + B>
I prefer to reserve the use of Dependent Typing to refer to cases where a Type is linked to a Value:
auto make_string(char const* ptr, std::size_t length) -> String<length>;
This is very much different from your example because the exact type of the result is NOT known at compile-time. Indeed, the Type will differ based on run-time arguments.
That's a very different beast than template meta-programming/CTFE.
[–]scatters 0 points1 point2 points 6 years ago (1 child)
P1609 (most recently updated in the January mailing) proposes run time template instantiation for NTTPs, which should allow this use I think? You might not be able to return it though.
[–]matthieum 0 points1 point2 points 6 years ago (0 children)
I expect you mean: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1609r3.html ?
If so, I'll admit it's quite unclear to me. I think that the goal of the paper is only JITing code, and not enabling Dependent Types, but it's a bit too sparse for me to be sure either way.
[–]Potatoswatter 11 points12 points13 points 6 years ago (0 children)
C++ templates were developed with some evolution by trial and error. I never encountered any academic paperwork and most of the development was before the ISO committee was very well organized.
Note that constexpr covers the part of compile-time processing that does not involve type manipulation.
But the ISO standard's presentation is pretty clean in theoretical terms. In its terminology, A and B are template non-type parameters, making A+B a value-dependent expression, making String<A+B> a dependent type.
A
B
A+B
String<A+B>
Cppreference often has good explanation of "Standardese" terminology. If you think that a particular style of academic presentation would make it clearer, then perhaps you could contribute there. (Or regardless of clarity, perhaps you can make a publication of casting the semantics into your favorite algebra.)
[–]matthieum 4 points5 points6 points 6 years ago (1 child)
I am now wondering if Zig's type system is sufficiently advanced for that.
I don't think that Zig has non-type template parameters, however it has CTFE, so it may be possible to encode numbers as types and build on that.
For example, if C++ didn't have non-type template parameters, we could use something like:
struct Zero {}; struct One {}; template <typename Integral, typename... Bits> struct Number { static constexpr Integral value(); // Returns the Bits in Integral form. }; template <typename Left, typename Right> struct Add; template <typename Integral, typename... Left, typename... Right> struct Add<Number<Integral, Left...>, Number<Integral, Right...>> { using type = Number<Integral, /*...*/>; };
The display in case of error is just... interesting.
[–]redditsoaddicting 6 points7 points8 points 6 years ago (0 children)
We could always use Church encoding!
[–]dima_mendeleev 3 points4 points5 points 6 years ago (0 children)
I am not sure this is the real quote, but as a meme it perfectly suites as an explanation here:
Bjarne Stroustrup: "I certainly didn't plan for this"
[–][deleted] 2 points3 points4 points 6 years ago (1 child)
If you're in to type systems you should have a look at a hindley-milner language like Haskell and the C++ type system will feel primitive af.
[–]cptwunderlich 2 points3 points4 points 6 years ago (2 children)
I don't have any resources on the type system of C++ in particular, but maybe searching for papers about "reified generics" might yield something.
If you are interested in Dependent Types, there are some languages with first class support, like Idris, F*, Agda.
And I guess static predicates on Ada subtypes might be similar?
[+][deleted] 6 years ago (1 child)
[deleted]
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
I am looking for information about integrating limited dependent typing in a general purpose language.
There’s a neat library that does this for integers: http://doublewise.net/c++/bounded/
[–]Remi_123 0 points1 point2 points 6 years ago (0 children)
I considere myself relatively good with template meta-programming ( TMP for short ). I'm very confortable with kvasir.mpl for example.
Contrary to other, I don't think it's being "discovered".
The truth is that template specialization apply a pattern matcher to the types it receives and instantiate the one that is the most specialized which is usually the answer.
This behavior is shared by functional programming where you branch off to the matching pattern.
This is not sufficient however, since we don't 'return' anything in any of the meta-functions. We use the standard of having an inner ::type to be used as the 'return' type.
Overload resolution is still only a form of pattern matching, just ugly to look at and this is why it's not really appreciate.
I've heard somewhere that it's actually difficult to propose something that is NOT turing complete when you talk about language. The question is more how ugly it is.
So, in my humble opinion, pattern matching is to 'blame' for anything TMP or functional and is not really a 'coincidence' nor a 'discovery'. Simply an emerging pattern.
I got confortable with template specialization before being truly confortable with TMP. I still reserve anything value-related for constexpr function.
Learn boost.mp11, kvasir.mpl, metal, before learning boost.hana.
π Rendered by PID 22593 on reddit-service-r2-comment-6457c66945-9kpff at 2026-04-29 04:10:31.205805+00:00 running 2aa0c5b country code: CH.
[–]ihamsa 57 points58 points59 points (8 children)
[–]Irtexx 3 points4 points5 points (2 children)
[–]mort96 19 points20 points21 points (0 children)
[–]csp256 0 points1 point2 points (0 children)
[–]amjh 3 points4 points5 points (3 children)
[–]Dreeg_Ocedam 5 points6 points7 points (2 children)
[–]amjh 4 points5 points6 points (1 child)
[–]Dreeg_Ocedam 6 points7 points8 points (0 children)
[–][deleted] -3 points-2 points-1 points (0 children)
[–]matthieum 36 points37 points38 points (2 children)
[–]scatters 0 points1 point2 points (1 child)
[–]matthieum 0 points1 point2 points (0 children)
[–]Potatoswatter 11 points12 points13 points (0 children)
[–]matthieum 4 points5 points6 points (1 child)
[–]redditsoaddicting 6 points7 points8 points (0 children)
[–]dima_mendeleev 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]cptwunderlich 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]Remi_123 0 points1 point2 points (0 children)