C++ fluent builder pattern
The builder usage is very clean isn’t it? But the question is why it works. The
ctx.htmlBuilder() expression creates a temporary
HTMLBuilder object, then we keep calling those builder functions which returns the reference of the builder itself. The key here is, the temporary builder object lives to the end of the statement, so that we can chain the function calls without worrying about dangling pointers.
Every object and reference has a lifetime, which is a runtime property: for any object or reference, there is a point…
All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation. This is true even if that evaluation ends in throwing an exception.
There are two exceptions from that:
The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.
The lifetime of a temporary object created when evaluating the default arguments of a default or copy constructor used to initialize an element of an array ends before the next element of the array begins initialization.
We can log the order of ctor and dtor to verify this behavior:
<title>Welcome to C++ dev</title>
~HTMLBuilder() is called after constructors of
That’s all for today! Welcome for comments~