Christopher Smith on 17 May 2003 01:29:01 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] Chris' stupid C++ trick of the week


On Fri, 2003-05-16 at 18:13, Jon Stewart wrote:
> Hmmm... what if you wrapped the function?
> 
> template<typename T>
> void doFooImpl(T &foo)
> {
> 	// do lots of stuff with/to foo
> }
> 
> template<typename T>
> inline T& doFoo(T &foo)
> {
> 	doFooImpl(foo);
> 	return foo;
> }
> 
> That way the compiler would be mighty tempted to heed your advice about 
> inlining.

So, aside from this being a tad ugly and not actually guaranteed to work
(although I guess from a practical standpoint you can assume the
function will always inline), this would likely work for the case where
the "return" is purely for purposes of syntactic sugar, however, for the
case I mentioned with the returned ref *may or may not be* the same as
the ref argument, this will not work. Again a trivial example (you'd
need code more complex than this to convince most compilers not to
inline):

template<typename T>
T& doFoo(T& foo) {
    static T PIVOT(foo);

    return (foo < PIVOT) ? foo : PIVOT;
}

The idea is that for cases where the logic of what to return is
intrinsic to the actual "doing of foo", it would be difficult to
separate it from the core behavior of the function enough to be able to
force inlining as you've described.
-- 
Christopher Smith <x@xxxxxxxx>
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp