Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"05-27-12 - Prefer Push-Restore to Push-Pop"

3 Comments -

1 – 3 of 3
Blogger castano said...

On a similar note, instead of having parallel data structures like:

std::vector stack;

struct Scope {
Scope(...) {
stack.push(Data(...));
}
~Scope() {
stack.pop();
}
};

or if you prefer:

struct Scope {
Scope(...) {
restore = stack.size();
stack.push(Data(...));
}
~Scope() {
stack.resize(restore);
}
int restore;
};

Often a better approach is to simply use the program stack:

struct Scope {
Scope * next;
Data data;
};

Scope * top = NULL;

Scope::Scope(...) : data(...) {
next = top;
top = this;
}
Scope::~Scope() {
top = next;
}

May 31, 2012 at 11:22 AM

Blogger Jonathan said...

For this particular example, castano has the right idea, but I'm not buying your argument against a scope class either.

If you were using such a technique you would write it such that the scope class is the only API available. A potential bad implementer of Func2 can't subvert that without active malice on their part.

June 1, 2012 at 2:57 AM

Blogger cbloom said...

@ Jonathan -

It doesn't require active malice to break.

I've seen it many times with enables/disables.

Things like profilers, memory trackers, etc. usually get enabled & disabled.

You wind up having incredibly fragile rules, like "make sure you don't put a profile scope around the key-processing function because there's a key that can enable/disable the profiler".

September 9, 2014 at 10:58 AM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.