C++ nested designated initializer with pointer

  c++, c++20

I thought it would be useful to be possible to initialize a struct that references another struct with a pointer, using the designated initializer. Such a pattern often happens in some APIs (e.g., Vulkan). For instance, consider the following structs:

struct A {
    int v;
};

struct B {
    int v;
    A* a;
};

struct C {
    B* b;
};

and a function taking C as an argument

void func(const C& c) {}

I came up with an approach using unmove() (ref) function that converts rvalue to lvalue:

template <typename T>
T& unmove(T&& v) { return v; }

Using this function, the nested initialization can be written by

func({
    .b = &unmove(B{
        .v = 1,
        .a = &unmove(A{
            .v = 2
        })
    })
});

My question: Is it a valid usage of the function in terms of the standard? Or am I missing something?

Source: Windows Questions C++

LEAVE A COMMENT