Jon Stewart on 17 May 2003 02:53:01 -0000


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

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


> 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


Hmm. You seem to have nailed my brand of code right off the bat. A tad 
ugly, and not actually guaranteed to work. :-)


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


Yes. Breaking const correctness is too evil for words(*). Hiding copy 
constructors would be the way to go. 

If there were some stupid little way to manipulate the parameter as an 
r-value in a way that would only work for references, then that would make 
the compiler sit down. But I can't think of anything.

I'll read some Stroustrup...


Jon

* Also: my code is always const correct, even if it shouldn't be.
-- 
Jon Stewart
stew1@xxxxxxxxxxx
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp