Re:What if anything have we learned from C++? [video]
by pjc50 on Friday July 24, 2015 @06:55AM
(#9941120)
C has a generic: "void *". It just doesn't have any type safety.
Re:What if anything have we learned from C++? [video]
by eutectic on Friday July 24, 2015 @08:17AM
(#9941396)
It also has bad performance if you need to pass function pointers around.
Re:What if anything have we learned from C++? [video]
by pjc50 on Friday July 24, 2015 @09:20AM
(#9941746)
Is this noticeably worse than calling through the vtable?
Re:What if anything have we learned from C++? [video]
by eots on Friday July 24, 2015 @10:16AM
(#9942118)
In C++ generics don't involve calling through a vtable.
Re:What if anything have we learned from C++? [video]
by ksherlock on Friday July 24, 2015 @08:26AM
(#9941432)
C11 has type generic macros. You can build your own with the _Generic statement.
int maxi(int, int);
double: maxd(double, double);
...
#define max(x) _Generic((x), int: maxi, double: maxd, ...default: maxi)
Re:What if anything have we learned from C++? [video]
by f00fc0d3 on Friday July 24, 2015 @09:32AM
(#9941824)
Yeah, but you still need to write those maxi, maxd :-(
Re:What if anything have we learned from C++? [video]
by stinos on Friday July 24, 2015 @06:48AM
(#9941099)
I am not talking about C++ template nonsense, but not having to write max element search for every primitive type
How is it template 'nonsense', when those templates do exactly that?
Re:What if anything have we learned from C++? [video]
by f00fc0d3 on Friday July 24, 2015 @07:27AM
(#9941197)
Template metaprograming is nonsense, not simple type substitution.
C++ templates are just too much, especially when mixed with substandard developers.
Re:What if anything have we learned from C++? [video]
by bluetomcat on Friday July 24, 2015 @06:42AM
(#9941080)
Using statement expressions and the typeof extension, you are able to write function-like, type-generic, scope-insensitive macros like this one:
#define MAX(a,b) ({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; \
})
The inability to write template functions can be compensated by designing your function so that it takes a function pointer where the type-specific action occurs.
Re:What if anything have we learned from C++? [video]
by nly on Friday July 24, 2015 @07:55AM
(#9941308)
Your MAX has no respect for namespaces or scopes. A real world macro is probably going to be called MYLIBRARY_MAX to avoid this. Secondly the C++ lambda solution is just easier to read.
auto max = [](auto a, auto b) {
return (a > b) ? a : b;
};
max (3.1415, 42); // returns double
max (42ul, 78u); // returns unsigned long
max (17, 16); // returns int
Re:What if anything have we learned from C++? [video]
by f00fc0d3 on Friday July 24, 2015 @07:25AM
(#9941194)
Typeof is not standard and many DSP compilers do not implement that.
Secondly I meant max search in array, not between to scalar values which is trivial. I really don't want to have for(...) inlined everywhere.
I am working in DSP project which was started with typical C++ OO BS, finally with ended up with C compiled as C++ + some simple templates for cases as above.