by z_open · 2y ago · 0 comments
Nah those are easy to name, just annoying, it's types which literally don't have an external / public name, like lambdas, or locally defined types.
#include <iostream>
auto createVoldemortType(int value) {
struct Voldemort {
int value;
};
return Voldemort{value};
}
int main() {
auto voldemort = createVoldemortType(7);
std::cout << voldemort.value << std::endl; // output: 7
}I don't see any code ther that does that. Is it implicitly passed?
I don't know C++, though I did know C somewhat well earlier.
Ha ha, that reminds me of that phrase of yore, "the quality without a name (qwan)" (google it), which was heavily bandied about years ago, during the heyday of C++ and the software patterns movement (which continued a lot in the days of Java, of course). James Coplien (IIRC) and others of that time come to mind.
https://en.m.wikipedia.org/wiki/The_Timeless_Way_of_Building
https://en.m.wikipedia.org/wiki/Pattern_language
https://en.m.wikipedia.org/wiki/Jim_Coplien
Though I read a fair amount about that stuff, a lot of of it went over my head, but later, I did understand some of the patterns, after reading the design patterns book, and trying out some of them.
The template method pattern is my favourite pattern, because I understand it more well than many of the others :), and also because it is the basis of software frameworks (inversion of control, aka the Hollywood principle - "don't call me, I'll call you"). Other patterns that I like and understand are the command pattern, the interpreter pattern, the chain of responsibility pattern, and flyweight pattern, to name a few. Builder and Factory, not so much. Singleton is straightforward, or is it really? impls matter :)
And I have written a few toy frameworks, which is fun to do and use.
I agree that you don't need auto pointers in C the way you do in C++. C++ type names can get so cumbersome...much easier to let the compiler figure it out for you.
#define SWAP(var1, var2) do { \
auto tmp = var1; \
var1 = var2; \
var2 = tmp; \
} while (0)
Previously, you would have needed a third macro for the type, or you would have needed to do a byteswap to be generic. const typeof(var1) tmp = var1;Consider this:
if (e)
SWAP(a, b);
else
something_else();
Currently, that expands to this, which is still valid code: if (e)
do {
// ...macro...
} while(0);
else
something_else();
If it were just wrapped in braces, the code would be parsed like this: // One-armed if-statement
if (e) {
// ...macro...
}
// Empty statement
;
// Another statement
else something_else();
It's incorrect syntax to start a statement with else, so the compiler will say something like "Unexpected 'else' at line N.""What's the best way to write a multi-statement macro?"
SWAP(…);
I think they obfuscate things unnecessarily. In C++ it's understandable because with templates you have a tendency to have massively complicated type names.
Everything else looks great to me though.