在函数调用中定义一个匿名结构

Define an anonymous struct in function call

我已经可以做到了:

struct {
    uint64 _i;
    bool operator()(uint64 elem)
    {
        const uint64 i = _i++; return elem & i; // Just an example
    }
} filter;

// Templated function
Array<uint64> clone = Containers::filter(array, filter);

我想知道是否可以在函数调用中移动结构定义,这样我就可以定义一个像这样的宏:

Array<uint64> clone = Containers::filter(array, ENUMERATE(i, elem, elem & i)); // Same as above

当我尝试这个时,我得到 expected primary-expression before 'struct':

Array<uint64> clone = Containers::filter(array, struct {
    uint64 _i;
    bool operator()(uint64 elem)
    {
        const uint64 i = _i++; return elem & i;
    }
});

有状态的可变 lambda 可以满足您的要求:

Array<uint64> clone = Containers::filter(
    array,
    [_i = uint64{0}](uint64 elem) mutable -> bool {
        const uint64 i = _i++;
        return elem & i;
    }
);