3 ms·
You haven't demonstrated the capability of Generics though (which C doesn't really have, only a minimal version). For this particular example, C++ does function
by npalli 2y ago
You haven't demonstrated the capability of Generics though (which C doesn't really have, only a minimal version). For this particular example, C++ does function overloading on parameters which C cannot hence you need this silly Generic macro hack, it is not clear how much of a win it is. Reminds of the saying that C is the most dynamic programming language since it has void*
It is much cleaner in C++ where you don't need to declare two separate functions names like you did.
#include <iostream>
#include <cmath>
struct Rectangle {
double width;
double height;
};
struct Circle {
double radius;
};
double area(const Rectangle& rect) {
return rect.width * rect.height;
}
double area(const Circle& circle) {
return M_PI * circle.radius * circle.radius;
}
int main() {
Rectangle rect{5.0, 3.0};
Circle circle{2.5};
std::cout << "Rectangle area: " << area(rect) << std::endl;
std::cout << "Circle area: " << area(circle) << std::endl;
return 0;
}