|
Boost : |
From: Justin M. Lewis (boost_at_[hidden])
Date: 2003-04-23 00:24:17
I don't really know how this works. I know someone who's a big Boost fan, and he said he didn't know of anything like what I'm going to propose exists in Boost here. So, here it is.
The problem I was having, we have references, that allows me to pass a paramter to a function, that function modifies the value passed in, at the point where that function is called, I cannot tell that the data is being modified. Basically, it leads to somewhat confusing code, especially in a case where you're looking at someone else's code trying to track down where a value gets set.
void func(int &x){x = 1977;};
void blah()
{
int y=0;
func(y);
func2(y);
printf("%d\n", y);
}
Something along those lines could become confusing, obviously in a much larger program.
My idea was, if you make a simple template class, originally I called it CRetVal, you could force people to specify at the time the function is called what's going on. I made a helper template function retval that would create and return a CRetVal object of the desired type to save from needing to use template notation in line all over the place. So, the above would become something more like.
void func(CRetVal<int> x){x = 1977;};
void blah()
{
int y=0;
func(retval(y));
func2(y);
printf("%d\n", y);
}
After talking to some people about the idea on #c++ on the Undernet, it was refined a little, and split into 2 classes, c_in_out, and c_out, with 2 helper functions, in_out, and out.
I don't know if anyone would have any interest in this. But, I think in a large project, it would make tracking down where values get set and changed a LOT easier. It saves you from having to lookup the prototype, and, (if you happen to work with people who don't seem to know of the existence of const) the definition of every function you're looking at.
So, I suppose, I'd like to get some feedback, I've got 2 classes all worked out, I'm not sure what I'd do with them, but the basics are there and ready to go.
Thanks
Justin M. Lewis
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk