Re: Deterministic functions
Dave Harris escribi=F3:
alvaro.segura.SPAMFREE@gmail.com (Alvaro Segura) wrote (abridged):
I propose to have a special keyword to mark functions that will return
the same value if the same arguments are given AND if only functions o=
f
a special kind (probably const) are called on the object.
class Container {
public:
int size() const deterministic;
Item& getAt(index i) const deterministic;
void add(Item item);
};
How would you deal with aliasing?
static Container container;
void proc( const Container &c ) {
for (int i = 0; i < c.size(); ++i)
container.add( c.getAt(i) );
}
Should the compiler assume that c is not a reference to container? Or m=
ust
it suppress the optimisation? Or is this undefined behaviour?
-- Dave Harris, Nottingham, UK.
Oh! Got me.
I guess it should not optimize just in case they are the same. Or how do
compilers deal with other aliasing problems? IIRC there are flags like
"assume-no-aliasing" in some compilers to make them optimize things they
could not otherwise.
The optimization (caching) should be done only if no non-const functions
are called on *that* object. If there is a call to a non-const and it
can't be sure if they are the same or a different object, then it should
make the function call again normally.
I think there are currently similar conditions in the autovectorization
optimization in GCC, where possible aliasing disables lots of potential
optimizations.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with=
]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu =
]
[ --- Please see the FAQ before posting. --- =
]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html =
]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]