Daniel K.
Daniel K.

Reputation: 987

C++ compiler optimization

Will modern C++ compilers (including gcc) optimize the following (macro-like) code?

    template<typename F, typename ...A>
    err foo(F fn, A&&... args)
    {
        return fn(std::forward<A>(args)...) ? get_last_error() : err();
    }

For example, can it optimize the following code

    return foo(test, 5, 20, "bar");

to:

    return test(5, 20, "bar") ? get_last_error() : err();

Upvotes: 1

Views: 337

Answers (1)

chisophugis
chisophugis

Reputation: 799

What you're describing is called "perfect forwarding" (that's the term to google if you want to learn more in depth about it), and C++11 supports it thanks to r-value references. So the answer is yes, it will get fully optimized.

Upvotes: 2

Related Questions