Connecting Tech Pros Worldwide Help | Site Map

Re: Trick needed - passing a large number of parameters to a class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2008, 06:25 AM
tony_in_da_uk@yahoo.co.uk
Guest
 
Posts: n/a
Default Re: Trick needed - passing a large number of parameters to a class

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...
}

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.