|
-
November 10th, 2009, 11:44 AM
#5
Re: howto restric a method to being absolutely constant
I know const can be overridden in more than one way but using const is at least a very strong reminder that an object should only be read. The main problem I have with a plain const method is that it can still change the state of objects its class has a pointer to. I would like an "absolute_const" keyword to declare that a method can only access pointers as if they were declared as pointers to constant objects ( const TYPE *p; ). So basically an absolute_const method could only change the state of its arguments.
First notice that
Code:
virtual void operator(Target &target) absolute_const;
in my class TargetedFunctor is a virtual function so it's behaviour is undefined for thread 2 that is going to process the functor at some time in the future.
The main problem I'm concerned about is that some class D, derived from my class TargetedFunctor, can have a pointer to some object X and can write to X in "operator()", called by thread 2, when it is concurrently being accessed by thread 1. Or that it could pass a pointer to "target" on to object X and that "target", from that point on, can also get accessed by thread 1.
If operator() was declared to be "absolutely_const" then it could not do this. It could only read from objects is has a reference to and write to argument target. Unless it was dowcasted or some things were declared as mutable .
So does absolute_const exist or can I construct something that is just as restrictive?
One alternative I thought of was to template the use of TargetedFunctor and then let my "template<F> Queue" class instantiate functors of type F. However I would need to be able to provide an argument object for the construction. And this argument object could very well contain pointers. So this doesn't work unless I can declare that this argument can't contain any pointers which, I think, is also impossible in C++.
Code:
template<F, FA>
class Queue{
public:
void scheduleFunctor(FA argument, Time when){
F f = F(argument);
....
}
};
I hope this clarifies my problem.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|