Jan 13, 2016

[C++] Lambda issues

https://www.reddit.com/r/cpp/comments/40lm8o/lambdas_are_dangerous/
https://www.reddit.com/r/cpp/comments/40scxe/jrbprogramming_a_workaround_for_lambda_odr/

A potential code bloat could be caused by using lambda for template arguments.

However, there's another issue, violating ODR.

Read on the reference, jot down result later...

--------------
Update:
Code taken from: A Workaround for Lambda ODR Violations
 // Based on Richard Smith trick for constexpr lambda
    // via Paul Fultz II (http://pfultz2.com/blog/2014/09/02/static-lambda/)
    template<typename T>
    auto addr(T &&t)
    {
        return &t;
    }

    static const constexpr auto odr_helper = true ? nullptr : addr([](){});

    template <class T = decltype(odr_helper)>
    inline void g() {
        int arr[2] = {};
        std::for_each(arr, arr+2, [] (int i) {std::cout << i << ' ';});
    }


It's still an ODR violation, which is that,
any inline function in the header spread into different TU that
calls g() will have different definition, due to any lambda expression
in a single TU has a different type.


Reference:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.