by stupang · 10y ago · 0 comments
Is that common among programmers?
It's doesn't hurt either that the libraries available in C++ are really cool.
I find it to be the case with most languages.
> Even template metaprogramming alone is gigantic area of ongoing research -- the stuff you can do with it is mind-bending and unavailable in nearly all other languages.
Your are being a bit hyperbolic. Meta-programming is not necessarily a hard thing to do, but in some languages, like C++ it was made impractical at first. Then, templates introduced some metaprogramming aspects with another layer of Turing-completeness. I can appreciate the usefulness of C++ and how templates contribute to it, though.
> Templates are, after all, one of very very few wholly functional, immutable languages out there.
XSLT too ;-)
> And template instantiation is the underlying force behind many of the modern features like lambdas that allow you to access that power without actually knowing templates well.
Lambdas roamed the earth long before templates.
> ... and if you can accept that you will never know the entire language and can just use those bits that make things easier, it is possible to have a healthy relationship with the language.
Agree.
I'm talking about C++ lambdas, in particular, which are templates under the hood.
Um, no. C++ can be safer than C (which is an incredibly low bar) but only if you carefully constrain yourself to certain subsets of the language, which is why the web is chock-a-block with byzantine "C++ coding standards" documents describing all the rules you have to follow in order to avoid shooting yourself in the foot.
"Avoid the parts you don't like" works best for one programmer working alone.
However, those things exist in the language. You can't just pretend nobody is going to use them, because there might be very good reasons to use them, especially in an imperative context.
(Though I suppose if you're grading 100 projects, such a rule might make it easier to get the code structured the same everywhere.)
I'm curious which subsets are included in this?
https://github.com/isocpp/CppCoreGuidelines
So not the sort of thing one can answer in-line in an HN comment.
As a developer responsible for understanding what everyone else is doing on the same project, no I cannot accept that.
Dislike Objective-C verbosity and C roots, although I kind of like its Smalltalk influence.
Really strongly dislike C and the culture of insecure software it has brought into our industry.
The majority of C++ programmers make use of:
- RAII
- String classes instead of char* überall
- Vector classes instead of char* überall
- new and delete instead of malloc()/free() and casts
- References for out parameters instead of pointers
- Real enums, specially since C++11, instead of #define
- Smart pointers for memory management
- Type based programming to reduce errors
- Minimize the use of the pre-processor to #include and macros, only when they cannot be expressed via templates, const, constexpr and inline.
Most of the unsafe C++ code is written by the "C with C++" sub-community that are mostly C refugees forced to use a C++ compiler.
Also while the C++ community is pushing for safety with activities like the C++ Core Guidelines and reducing the amount of UB in the standard, the C standard community doesn't care at all.
The only thing that the C community has done regarding security was the Annex K in C99, which was proven so good that it became an optional feature in C11.
Where is this modern C++ culture that has resulted in large-scale codebases free of memory safety vulnerabilities? I've never seen even one, much less a "widespread culture".
> The majority of C++ programmers make use of:
None of this, empirically, results in safe code.
> Most of the unsafe C++ code is written by the "C with C++" sub-community that are mostly C refugees forced to use a C++ compiler.
I haven't seen any evidence for this. A lot of vulnerabilities are found in modern C++11.
> Also while the C++ community is pushing for safety with activities like the C++ Core Guidelines and reducing the amount of UB in the standard, the C standard community doesn't care at all.
I don't see any way that the C++ Core Guidelines are going to be able to succeed in creating a usable language that is memory-safe. Robert O'Callahan and I have elaborated why in other posts.
Can you name one C++ program with the same track record as postfix or qmail?
RAII and scope based construction/destruction is actually quite nice.
This is a problem with vocabulary types. C tends to do a good job of providing them. C++ tends not to, though there has been some progress in the standard lately in defining classes like unique_ptr and string_view.
C tends to fall down by providing raw memory (an int and a char) to represent fairly subtle relationships (the int represents the size of the string pointed to by the char... what happens when the int is negative?). So C libraries don't 'reinvent the whole stack', but this sort of bookkeeping logic tends to cause bugs (at least security bugs).
...but I get confused by the people who do that, and then turn around to embrace C, the circus where people juggling the chainsaws of "faking templates with macros", "terrible build times", "yet more security vulnerabilities resulting from undefined behavior", "no C-standard defined threading / async support until C1X", and more.
It's common, but I don't get it.
I find it extremely frustrating when I have to use either language in an area that it wasn't designed for.
// file: A.h
class A {
B* _b;
};
// file: B.h
class B {
A* _a;
};
I mean a pointer has a fixed size, so couldn't the compiler just leave some sort of type placeholder in class A until class B is eventually defined. // Things.h
namespace Foo { class A { C* c; }; }
namespace Bar { class B { C* c; }; }
Are A::c and B::c the same type? // TU1.cpp
class C {};
Were you right? // TU2.cpp
namespace Foo { class C {}; }
namespace Bar { class C {}; }
What about now? // TU3.cpp
class C {};
namespace Foo { class C {}; }
namespace Bar { class C {}; }
This may get you shot, but is legal. What about now?Ahh - all three are in the same program. Just pretend they #included <c.h>, <foo/c.h>, and <bar/c.h>. Having A::c's type be different in different contexts is a violation of the "One Definition Rule", for which the punishment is undefined behavior.
Although you can still recreate the above scenarios by just #including <Things.h> after <c.h> and company, but you're a bit more likely to have Things.h #include what it needs to clarify the situation. Hopefully. So maybe we can get away with it!
void foo() { D * d; }
Is that a pointer definition or invoking operator* against two globals? void bar() { E < 42 > e; }
Now you're just fucking with me. Is that some comparison operators or a template? void baz() { F < (42) > f; }
That didn't clarify anything. Stop it. void he_comes() { G < (42) > g(); }
What do you mean I just declared a function? Stop it! v̹̊̈́̇ͦ̌ͭ͂ͅo̅̉̀ͪ̚ī̵̠̘̋d̸͚̪̝̹͙͆͛̿ ͉̜̳ͪͬ́ͣ̌ẑ̸̹̹̩̩a̡̰ͣͭ̄͒ͫ̇̚l̛̹̫͓̣͖͈̐ͬg̢͕̘͙o̪͆̏ͥ͟(̖̭̟̱̰ͩ)̘̬͉̺̉̓ ̦͆́̚{ ̶̪͙̹͈ͮͧ̋ͪͤͅĜ͔̙̉͢ ̝̯ͭ̈́͘<̨͊̂͋̎ ̤͇̼̝͔͇ͯ̑̋ͫ̾͐̚(ͨͪͦ̈̈́ͥ4͍͗̆2̲̞̦̳̖͡)͖͇̟̥ͮ͘ ̺̖͙̘̳͛̐̊͊>̼͍̘̦͖̎ ̧̘̻̭͉̺g̟͓̑ͦ̌̃(̪ͬͪͤ̎̉͘ȉ̮̥͖́n̸̝̪̗̯͇̓̈́̀̔̄̍́t͓̟͍ͨͪ(̰͈̻͎̣͉̀̿̈͑)̝͚̆̃̇)̠̃;̺͎̙̰̈́̏̚͘ ̶̜͍̠̼ͬ}̡̤̰̮̳͈̌͛ͤ̈́
A function accepting a function pointer? Stooooop! // file: A.h
class B;
class A {
B* _b; // or any of the other variants.
}; class A {
class B* _b;
}; // file: A.h
class B;
class A {
B* _b;
};
// file: B.h
class B {
A* _a;
}; class B;
class A {
B* _b;
};
and it'll be happy.