I want to write a function that uses a lot of parameters, which I will call a, band c. I have four options for implementing this in C ++ 14.
For the new modern C ++ project in 2018, which of these styles best adheres to the ISO C ++ philosophy ? What styles are recommended by other style guides?
Object oriented style
class Computer {
int a, b, c;
public:
Computer(int a, int b, int c) : a(a), b(b), c(c) {}
int compute(int) const {
}
};
...
const Computer computer(a, b, c);
int result = computer.compute(123);
Pros:
- Easy for C ++ programmers to understand.
Minuses:
- To figure things out in map or fold operations, we have to make a clumsy
[computer](int input){ return computer.compute(input); }
Style C
struct ComputeParams {
int a, b, c;
};
int compute(const ComputeParams ¶ms, int input) {
}
...
const ComputeParams params{a, b, c};
int result = compute(params, 123);
Pros:
Minuses:
- Verbal implementation
computeincludes a call params.ainstead a. - Conditional calls must go through each time in the structure.
struct Computor {
int a, b, c;
int operator()(int input) const {
}
};
...
const Computor compute{a, b, c};
int result = compute(123);
:
:
auto genCompute(int a, int b, int c) {
return [a, b, c](int input) -> int {
}
}
...
auto compute = genCompute(a, b, c);
int result = compute(123);
:
- OCaml
- , map, fold for_each
- ,
:
- ++ C,
- - , ,
auto - std::function - vtables