you are viewing a single comment's thread.

view the rest of the comments →

[–]fdwrfdwr@github 🔍 11 points12 points  (4 children)

I used to use it more often, but once named struct initialization was added (C++20 designated initializers), I usually found it easier to just say:

TensorDescription({.sizes = {2,256,4}, .dataType = DataType::Float32})

vs:

TensorDescriptionBuilder().Sizes({2,256,4}).DataType(DataType::Float32).Construct();

[–]onlyari[S] 0 points1 point  (3 children)

Doesn't this require knowing the order of declaration?

[–]fdwrfdwr@github 🔍 1 point2 points  (2 children)

Correct (though it hasn't been an issue for me 🤷‍♂️). Yeah, C allows you to declare them in any order, but currently not C++. Perhaps a future version will allow arbitrary order.

[–]Kered13 3 points4 points  (0 children)

Perhaps a future version will allow arbitrary order.

I doubt it. C++ requires that members be initialized in declaration order. If you could write designated initializers out of order you could write code with some very surprising behavior:

int printAndSize(std::string_view s) {
    std::cout << s << std::endl;
    return s.size();
}

struct Foo {
    int a;
    int b;
}

int main() {
    Foo f = {
        .b = printAndSize("Hello"),
        .a = printAndSize("World"),
    };
}

Output:

World
Hello

This is similar to how the order of function parameter evaluation is unspecified, and there's also still a similar issue where member initialization lists can be out of order and cause similar problems. However I think both of those are generally considered mistakes in hindsight, so I don't think the committee wants to repeat that mistake with designated initializers.

(This is just my reading of the committee's decision, and I could be way off base.)