On Fri, 11 Mar 2016 19:41:22 +0000 (UTC)
paul Fultz
On Friday, March 11, 2016 12:30 PM, Bjorn Reese
wrote: On 03/11/2016 01:50 AM, Paul Fultz II wrote:
library. I do, however, need to discuss some of the misperceived issues with using global function objects in the documentation. As the issues raised in the review were:
The destruction order of global objects in different translation units is undefined. This means that I cannot use a global fit function in the destructor of one of my own global objects because the former may have
been destroyed before the latter.
This is not a problem, because it is initialized at compile-time, that means it is a literal type:
http://en.cppreference.com/w/cpp/concept/LiteralType
So besides the constructor not having side-effects, the destructor must be trivial, as well. For example, this cannot compile:
struct foo_fn { template
auto operator()(Ts&&...) const {} ~foo_fn() { std::cout << "Hello"; } };
BOOST_FIT_STATIC_FUNCTION(foo) = foo_fn();
int main() { foo(); }
So with trivial destructors, the order of destruction doesn't matter. Furthermore, on most compilers(that is everything but MSVC), the function is the same across all translation units. So there is only one function in the executable, not one per translation unit. Taking the address of the function will yield the same address across all translation units. This is the same way functions work.
The following program throws an exception and aborts in Clang 3.4 and
Gcc 4.8, both on Ubuntu 14.04
#include <iostream>
#include <stdexcept>
#include