Thread: Hopefully novice code question.

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    If the recommended solution is to use RAII, and catching exceptions from a constructor, cleaning up after the constructor, and rethrowing the exception follows RAII, then why is it not recommended? Is that not entirely your opinion?
    That is not the point. The constructor must clean up after itself, so why should the constructor be an exception to the RAII rule? If the code that uses the class uses raw pointers, and therefore makes it safe by wrapping them in smart pointers, then why should not a constructor do so?
    The constructor should not clean up by catching the exceptions and re-throwing them. It should rely on objects destructors--objects used inside the constructor--to clean up. That makes the constructor safe, and thus the class safe.
    Catching exceptions to clean up is cumbersome and dangerous--it can lead to many bugs. Plus it's expensive to rethrow an exception.
    It is like building blocks. Always use building blocks that clean up after themselves everywhere. A raw pointer does not clean up after itself, therefore it is dangerous. A smart pointer will always clean up if properly used, and is therefore safe to use. Even in constructors where you won't have to catch the exception to clean up by using them.

    I meant that shared pointer implementations also throw exceptions from constructors and call delete p before reraising it. So, since it is a used solution and you're going to mention it, I don't see how it's correct to say it's not recommended. Even if it's not recommended, it's in your opinion only.
    What would such a scenario be? I cannot think of such a scenario where a smart pointer would catch an exception only to delete its raw pointer. That would be silly, since the destructor would always take care of that.
    And no, it's still not my opinion. Catching and rethrowing exceptions is cumbersome and expensive, and is therefore generally not recommended when there are alternative solutions available. And in 99% of the cases, there are.

    Who said it wasn't used? People link to it all the time on here.
    Linked to is not the same as new articles created there, which is what I was referring to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What would such a scenario be? I cannot think of such a scenario where a smart pointer would catch an exception only to delete its raw pointer. That would be silly, since the destructor would always take care of that.
    If you throw an exception from the constructor, the class destructor will never be called. Only the destructors of member objects. And the raw pointer that a smart pointer does contain has a useless destructor.

    And no, it's still not my opinion. Catching and rethrowing exceptions is cumbersome and expensive, and is therefore generally not recommended when there are alternative solutions available. And in 99% of the cases, there are
    Perhaps I should be more stern: I don't care what's recommended, and it has to be damned wrong to say something isn't if that's the only thing people do in such a situation, no matter if its hidden away in a proper solution.

    Linked to is not the same as new articles created there, which is what I was referring to.
    Be the change that you want to see in the world. It's probably a good idea to be in the right when you do, though.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    If you throw an exception from the constructor, the class destructor will never be called. Only the destructors of member objects. And the raw pointer that a smart pointer does contain has a useless destructor.
    If you do anything that might throw an exception inside a smart pointer, then you would wrap your raw pointer in another smart pointer, such as std::unique_ptr. Thus, you do not need to catch the exceptions.

    Perhaps I should be more stern: I don't care what's recommended, and it has to be damned wrong to say something isn't if that's the only thing people do in such a situation, no matter if its hidden away in a proper solution.
    ...And it very likely isn't even used in such a situation. The proper solution is... yes, you guessed it, again, to use proper RAII techniques. Wrap all resources into containers and use Boost.ScopeExit to take care of cleaning up that needs to be done when the function exits.
    It isn't a widely used practice at all, to catch and rethrow exception to my knowledge.
    And even if it is, it's not a thing I would recommend--or teach--newbies.
    Even if it is just my opinion, it's not a bad opinion, and therefore I see no problem in letting people know about it. Everyone voices their opinions all the time. And this opinion is not bad practice.

    Be the change that you want to see in the world. It's probably a good idea to be in the right when you do, though.
    What is right to me is not necessarily right to you. You cannot expect everything written by me--or anyone else--to be free of criticism.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    ...And it very likely isn't even used in such a situation. The proper solution is... yes, you guessed it, again, to use proper RAII techniques. Wrap all resources into containers and use Boost.ScopeExit to take care of cleaning up that needs to be done when the function exits.
    It isn't a widely used practice at all, to catch and rethrow exception to my knowledge.
    Well it must be. I'm insisting that smart pointers themselves, in their simplest implementation, use the very technique you denounce to make their code exception safe. Boost is one example. Only the default constructors do not throw.

    I don't see the point in your insistence that its bad to teach a working, RAII following, standards reliant solution that could very well be used, and to my knowledge is used, at some level in so called safer things, in addition to the other crap you could use.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Well it must be. I'm insisting that smart pointers themselves, in their simplest implementation, use the very technique you denounce to make their code exception safe. Boost is one example. Only the default constructors do not throw.
    Smart pointers in their simplest implementation most likely do not require this technique as they really should not be able to do anything that throws.

    I don't see the point in your insistence that its bad to teach a working, RAII following, standards reliant solution that could very well be used, and to my knowledge is used, at some level in so called safer things, in addition to the other crap you could use.
    All in due time. You do not start with the complex things first. You start with the easy things first, then generalize it.
    What someone doesn't know cannot cause any harm.

    But as to the initial thing that sparked this... in situation with raw pointers, I do claim that catching and throwing is not a good solution. Especially not in the example.
    From what I understand, that was one beef you had with the article.

    I have reworded some things in the article. Perhaps you will this wording better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have reworded some things in the article. Perhaps you will this wording better.
    You've done nothing to appease me.

    But my issue with the wiki as a whole is that its essentially your opinion dump now, and you publish entire articles and other people only find out about it when you link to them, because you also refuse to integrate your crap onto the wiki, like say on the main page. Not that it matters, because no one edits the wiki anymore, and thus, no one is there to keep you honest.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I do put links on the main page. When I write new things, it is because I do not believe there is a suitable other place.
    Considering a lot of people are registered and can edit the wiki, it is not my fault that no one is. I doubt that moving it elsewhere would help.

    So. We are at crossroads.
    If you keep denouncing my articles, I will keep denouncing your claims.
    What do we do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    I do put links on the main page.
    No you don't, and yet you link to them all the time: No one else knows about those pages until you do.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So it appears I remember wrong.
    And yet, I wonder if people will even read the main page from time to time, or even the changes page which can be used to spot changes.
    I could do it, but it would be of any benefit? If you would like me to do it in the future, I'll oblige, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A rather broad code design question
    By Walker in forum C++ Programming
    Replies: 6
    Last Post: 01-27-2010, 07:09 PM
  2. Novice Pointers/Class Question
    By C++Gamer in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2006, 05:36 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM