//For best effect, pipe stderr to a file
publicclass InfiniteException extends Throwable
{
publicstaticvoid main(String[] args) throws Throwable
{
thrownew InfiniteException();
}
@Override
public Throwable getCause()
{
returnnew InfiniteException();
}
}
I guess it's good that the implementation used a recursive approach or this really would be infinite! (Note: it magically detects if you returnthis; instead)
//deep in the call stack
thrownew ExitException();
/*...*/ main(/*...*/){
try{
dummy_main(/*...*/);
}catch (ExitException){
}
}
That's actually what Boost.coroutine does to give objects on the "fake" stack a chance to clean up when a coroutine object is destructed before the coroutine entry point has had a chance to return.
...Boost.coroutine does [that] to give objects on the "fake" stack a chance to clean up when a coroutine object is destructed before the coroutine entry point has had a chance to return.
It's pretty amazing what you can do with Boost. There's a guy who used Boost.MPL to introduce a "code feature" concept that ensures e.g. functions marked as thread-safe can only call other functions marked as thread-safe.
Hey, you're right, you could use std::tuple (or boost::tuple in this case, since the article was written in 2008). Perhaps boost::mpl::vector has some other features.
It would be great to have a language feature that could enforce thread-and-exception-safety. I would also like the const specifier to be applicable to static-and-non-member functions (or perhaps a "pure" specifier [gcc has __attribute__((pure)) already]) which enforces that functions do not modify global state. It would make writing safe C++ functions more easy.
I have always wanted more -correctness in C++ than just const-correctness. Const-correctness alone is amazing and I am always missing it in other languages, but I frequently want to enforce other requirements and control which are convertible to others and how. Maybe in the far future when we get past Concepts Lite we can have some fun.
Haskell is good for that. In order to do anything with side-effects you have to use a monad, and it is enforced by the compiler e.g. if your function doesn't use the IO monad it can't use I/O, and it can't call any functions that do (unless you use the liftIO function, which lets you transform the IO monad to a different one).
I suppose that in a way, abstraction is really what we're doing, except that instead of abstracting using classes and objects, we're abstracting using functions and contracts. Am I right in thinking that? Or is it something else since, for example, const-correctness is viral?
I have always wanted more -correctness in C++ than just const-correctness.
C++ doesn't have const-correctness. It has only half of it (meaning - you're not allowed to change the object but you're given no guarantee it cannot be changed by something else). Rust has full const-correctness.
The function display_number() promises not to change the state of the program via its parameter. It may change the state of the program in any number of other ways, however.