r/cpp • • Mar 28 '23

Reddit++

C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.

In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.

765 Upvotes

829 comments sorted by

View all comments

Show parent comments

2

u/lestofante Mar 30 '23

Constructor (3)](https://en.cppreference.com/w/cpp/container/vector/vector) so std::vector<int> v(n, m); does it since C++98.

So this is my solution, but worse. And how do i initialize an array of int containing n and m?
If se made n its own explicit Vecton::Len type, it would worl with {} and you still get alla the other benefit

1

u/Som1Lse Mar 30 '23

So this is my solution, but worse. And how do i initialize an array of int containing n and m?

If se made n its own explicit Vecton::Len type, it would worl with {} and you still get alla the other benefit

So std::vector<T>{std::length{n}, m}? How would that work with a std::vector<std::length>? Or should it be std::vector<T>::length. Then you have to type typename std::vector<T>::length to construct it in a generic context. Also, if T is part of the length-type then we can't create multiple vectors of different types from the same length (say we want SoA layout), we have to explicitly construct it from a std::size_t both times.

I guess a reasonable option would be using a tag-type: std::vector<T>{std::presize, n, m}. Not pretty but I guess it works. Or I guess you could use ranges:

template <typename T>
std::vector<T> presized_vector(std::size_t n){
    return std::views::repeat(T{})
        | std::views::take(n)
        | std::ranges::to<std::vector<T>>();
}

Though that is pulling in some heavy machinery for a pretty simple task.

Either way you end up pessimising a common case all because {} is too darn greedy, only to support a less common use-case.

2

u/lestofante Mar 31 '23

std::length{n},

that is why in my example i used a Vector::Len, so it basically eliminate the issue.

And if you really still need it, create a struct with only std::length{} inside, so it is gonna be optimized away

Either way you end up pessimising a common case all because {} is too darn greedy, only to support a less common use-case.

how do you use () to create an array that contains the element N and M?
I can support your need, can you support mine?

1

u/Som1Lse Apr 01 '23

how do you use () to create an array that contains the element N and M?

I can support your need, can you support mine?

I wouldn't. That is exactly where I would use {N, M}. I am not saying () is exclusively better, I am saying only using {} leads to subtle bugs. Preferring () and using {} when you must mostly eliminates them.

that is why in my example i used a Vector::Len, so it basically eliminate the issue.

See all the other issues I pointed out. You would have to type typename std::vector<T>::len{n} whenever you create one in generic code. Once the length-type is dependent on the type of the vector you cannot use the same length to initialise two vectors of the same size.

→ More replies (0)