On Jul 22, 5:27 am, eran <kale...@gmail.comwrote:
Quote:
... there are 3 ways of setting that [large] bunch of params:
- Pass them all as argument to one function
- Set each one of them
- Use a params struct
What bothers me is this: I'd really like to minimize the chance that
the caller will miss setting one of the params.
...
I'm also with the params struct w/ constructor approach, but just to
for a hoot I'll illustrate an alternative (pretty absurd) approach,
which successively changes the compiler's notion of type such that a
call only becomes possible when all parameters are set. ( Yes - enums
would have been better )
Cheers, Tony
// validate that all required parameters are supplied...
#include <iostream>
template <int flags = 0>
struct X
{
X<flags | 1>& a(int n) { a_ = n; return *(X<flags | 1>*)this; }
X<flags | 2>& b(int n) { b_ = n; return *(X<flags | 2>*)this; }
void do_something();
int a_;
int b_;
};
template <>
void X<3>::do_something() { std::cout << a_ << ' ' << b_ << '\n'; }
int main()
{
X<x;
x.a(2).b(4).do_something();
x.b(2).a(4).do_something();
x.a(1).validate(); // won't compile...
}