dynamic_cast<>. More...
#include "Teuchos_ConfigDefs.hpp"
Include dependency graph for Teuchos_dyn_cast.hpp:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Namespaces | |
| namespace | Teuchos |
Classes | |
| class | Teuchos::m_bad_cast |
Functions | |
| void | dyn_cast_throw_exception (const char type_from_name[], const char type_from_concr_name[], const char type_to_name[]) |
| template<class T_To, class T_From> | |
| T_To & | dyn_cast (T_From &from) |
Dynamic casting utility function meant to replace dynamic_cast<T&> by throwing a better documented error message. | |
dynamic_cast<>.
Definition in file Teuchos_dyn_cast.hpp.
|
||||||||||||||||
|
We throw a m_bad_cast, which is a subclass of bad_cast. This is necessary, since bad_cast lacks the appropriate constructor for use with the TEST_FOR_EXCEPTION macro. Definition at line 36 of file Teuchos_dyn_cast.cpp. |
|
||||||||||
|
Dynamic casting utility function meant to replace
Existing uses of the built-in
C &c = dynamic_cast<C&>(a); are easily replaced as:
C &c = dyn_cast<C>(a); and that is it. One could write a perl script to do this automatically.
This utility function is designed to cast an object reference of type Consider the following class hierarchy:
class A {}; class B : public A {}; class C : public A {}; Now consider the following program: int main( int argc, char* argv[] ) { B b; A &a = b; try { std::cout << "\nTrying: dynamic_cast<C&>(a);\n"; dynamic_cast<C&>(a); } catch( const std::bad_cast &e ) { std::cout << "\nCaught std::bad_cast exception e where e.what() = \"" << e.what() << "\"\n"; } try { std::cout << "\nTrying: Teuchos::dyn_cast<C>(a);\n"; Teuchos::dyn_cast<C>(a); } catch( const std::bad_cast &e ) { std::cout << "\nCaught std::bad_cast exception e where e.what() = \"" << e.what() << "\"\n"; } return 0; } The above program will print something that looks like (compiled with g++ for example):
Trying: dynamic_cast<C&>(a); Caught std::bad_cast exception e where e.what() = "St8bad_cast" Trying: Teuchos::dyn_cast<C>(a); Caught std::bad_cast exception e where e.what() = "../../../../packages/teuchos/src/Teuchos_dyn_cast.cpp:46: true: dyn_cast<1C>(1A) : Error, the object with the concrete type '1B' (passed in through the interface type '1A') does not support the interface '1C' and the dynamic cast failed!"
The above program shows that the standard implementation of
Note that this function is inlined and does not incur any significant runtime performance penalty over the raw Definition at line 149 of file Teuchos_dyn_cast.hpp. |
1.3.9.1