Jump to content
sumit_tuwien

SystemC with noexcept (c++11)

Recommended Posts

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

Share this post


Link to post
Share on other sites

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:
Best regards,
 
Torsten Maehne

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×