mardi 27 décembre 2016

Is dereferencing nullptr to lamdba function undefined behaviour?

As show by pfultz2 there is a work-around for static initialization of lambda functions. One of the steps mention dereferencing a nullptr of pointer to lambda function type.

template <typename T> typename std::remove_reference <T>::type * addr (T && t)
{
    return & t;
}

constexpr auto f = true ? nullptr : addr ([] (int arg) { return arg + 1; });

int main ()
{
    assert (((*f) (1) == 2));
}

Going through the spec and another question C/C++ nullptr dereference I have trouble understanding whether *f is undefined behaviour or not. What sections in the spec would make this not undefined behaviour?

Aucun commentaire:

Enregistrer un commentaire