$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: [boost] [function] "paranoia" vs efficiency
From: Domagoj Saric (domagoj.saric_at_[hidden])
Date: 2009-10-02 07:20:23
As far as I can see boost::function<>::operator() performs:
if (this->empty())
boost::throw_exception(bad_function_call());
before invoking the stored function (object)...
My question is why or why only this "checked"/"safe" approach/method of
invocation exists?
Because this:
- causes (extra) code branching
- causes the compiler to insert exception throwing and handling code in
surrounding/calling code (which need not otherwise be if the function stored
in the boost function instance is itself nothrow)
- inserts a call to the bad_function_call constructor
- disables the compiler from inlining the function (MSVC atleast)
- disables NVRO (MSVC atleast,
http://msdn.microsoft.com/en-us/library/ms364057(VS.80).aspx#nrvo_cpp05_topic3 )
while at the same time I would most often consider calling a
"null-function-pointer" a programmer error (that would therefor need to be
"handled" with an assert not an exception), not a runtime 'unpredictable'
exception...
...IMO the default should be not to include this check (i.e. use an assert)
and provide a safeInvoke() type of method for those users and cases that
actually require/want this type of check/behaviour...
...or atleast "it would be welcomed" if a non-checking unsafeInvoke() type
of method would be provided for those that are 'sure' that the
boost::function<> object is valid (for example in the line before i've just
performed the same check to decide whether to call the function object or
not)...
-- "That men do not learn very much from the lessons of history is the most important of all the lessons of history." Aldous Huxley