tea-age.solutions
Coder
C++ is a great language and for me personal the greatest programming language so far.
But there are also constructs possible which may look strange or really complicated.
Do you know some?
I wanna share a strange looking but sometimes very useful example:
Ever seen a such a try-catch block?
If this is new for you: This is a Functional Try Block.
This is useful if in the middle of the member initialization an exception is thrown and the half created class instance need to do some clean up. There is one special behavior in this construct. Do you know which?
The catch block will always re-throw the exception automatically because the class instance was not finally created and stays broken.
One complete example:
This example is from my last article of my blog with the title
TOP 5 most astounding C++20 syntax.
You will find 4 other examples there including a godbolt link for try all the code by your self.
https://tea-age.solutions/2022/12/16/top-5-most-astounding-cpp20-syntax/
Do you know other awesome syntax constructs?
Cheers and happy Coding!
But there are also constructs possible which may look strange or really complicated.
Do you know some?
I wanna share a strange looking but sometimes very useful example:
C++:
// [...] some missing code here to focus on the important part ...
// a construtcor of some class 'Constructor' (havig some structs/classes X, Y, Z as well)
explicit Constructor( X x, Y y, Z z )
try : mMember1( x ) // A functional try block around the member initilizer list
, mMember12( y, z )
{
} catch( ... ) {
}
// [...] some more code here ...
Ever seen a such a try-catch block?
If this is new for you: This is a Functional Try Block.
This is useful if in the middle of the member initialization an exception is thrown and the half created class instance need to do some clean up. There is one special behavior in this construct. Do you know which?
The catch block will always re-throw the exception automatically because the class instance was not finally created and stays broken.
One complete example:
C++:
class Foo
{
std::string mStr;
public:
// Constructor
explicit Foo( std::string const & s, size_t pos )
// # 4: Function try block. The try block includes the initializer list!
try : mStr( s, pos )
{
puts( "Foo constructor - normal block." );
} catch( ... ) {
// here can only cleanup already initialized/allocated stuff, which must be clean up manually.
// Constructed members are destructed already.
// the class instance itself stays broken. this catch block will auto re-throw the exception always!
puts( "Foo constructor - catch block!." );
}
};
try {
std::string s = "test";
Foo foo1( s, 2 ); // not throw
Foo foo2( s, 10 ); // will throw std::out_of_range
} catch( ... ) {
}
This example is from my last article of my blog with the title
TOP 5 most astounding C++20 syntax.
You will find 4 other examples there including a godbolt link for try all the code by your self.
https://tea-age.solutions/2022/12/16/top-5-most-astounding-cpp20-syntax/
Do you know other awesome syntax constructs?
Cheers and happy Coding!