sumit_tuwien Posted September 27, 2016 Report Share Posted September 27, 2016 Hello All, I want to understand usage of noexcept with SystemC. I read that using noexcept will provide me performance benefits! My doubt is how shall I label a member function noexcept, if it is using a SystemC or a library which throws. Following piece of code is self explanatory and needs assistance on correct usage of noexcept. # ifndef MYNOEXCEPTSCCLASS_H_ # define MYNOEXCEPTSCCLASS_H_ # include <systemc> # include <aLibraryThatThrows> template < typename Tinp , typename Tout > class myNoExceptSCClass : public sc_core::sc_module { private : static Tinp correctVersionForSure() { std::throw "Throwing for fun!" ; } void isThisCorrectVersion1() noexcept { sigOut.write(sigInp.read()); } void isThisCorrectVersion2() { sigOut.write(sigInp.read()); } void isThisCorrectVersion3() noexcept { sigOut.write(aLibraryThatThrows::functionThatThrows()); } void isThisCorrectVersion4() { sigOut.write(aLibraryThatThrows::functionThatThrows()); } public : sc_core::sc_in < Tinp > sigInp ; sc_core::sc_out < Tout > sigOut ; SC_HAS_PROCESS (myNoExceptSCClass) ; explicit myNoExceptSCClass (const sc_core::sc_module_name moduleName_) : sc_core::sc_module (moduleName_) , sigInp ("sigInp") , sigOut ("sigOut") { // Regular Stuffs } ~myNoExceptSCClass() final = default ; myNoExceptSCClass& operator=(const myNoExceptSCClass& other) = delete ; myNoExceptSCClass(const myNoExceptSCClass& other) = delete ; }; # endif Thanks in advance. Regards, Sumit Quote Link to comment Share on other sites More sharing options...
maehne Posted October 10, 2016 Report Share Posted October 10, 2016 Hello Sumit, my advice would be to not mark member functions as noexcept that contain calls to SystemC functions or a library that does throw. Otherwise, any uncaught exception leaving the context of the function noexcept-declared will yield a call to std::terminate(). Yes, annotating functions with noexcept may enable the compiler to do additional optimization. However, many modern compilers already generate code, which doesn't has a performance penalty in the path where no exception occurs. In case an exception occurs, you won't care about the performance penalty caused by the occuring stack unwinding. However, you do usually care about being able to deal with exceptions in places, where you have enough knowledge to properly deal with problem. This is not necessarily in the function you declared noexcept. So, in many cases you probably won't gain much by declaring your member functions noexcept. Still, there are certain places, where declaring functions as noexcept promises more advantages: in particular, move operations, swap, memory deallocation functions, and destructors. This is explained in much more detail in Item 14 of Scott Meyer's book "Effective Modern C++". The SystemC standard still is based on C++'03 and so is the proof-of-concept implementation provided by Accellera. It will require considerable work to move SystemC to actively use C++'11/14/17 features. The topic is on the agenda of the Language Working Group, as the discussion regarding this topic showed during the SystemC Evolution Day in Munich in May. Contributions through participation in the respective working groups are certainly welcome. Finally, to give you some more solid information regarding best practices for using noexcept, I would like to point you to: Scott Meyers "Effective Modern C++", Item 14 "Declare functions noexcept if they won't emit exceptions.", O'Reilly, 2015. Bjarne Stroustrup: "C++'11 FAQ: noexcept -- preventing exception propagation", 2015. Andrzej Krzemieński: "noexcept — what for?", Andrzej's C++ blog, 2014-04-24. Andrzej Krzemieński: "Using noexcept", Andrzej's C++ blog, 2011-06-10. Best regards, Torsten Maehne sumit_tuwien 1 Quote Link to comment Share on other sites More sharing options...
sumit_tuwien Posted October 11, 2016 Author Report Share Posted October 11, 2016 Hello Torsten, Thanks a lot for the detailed explanation. Regards, Sumit Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.