Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Check request: avoid dangerous ctad constructing container from iterators pair #121294

Open
denzor200 opened this issue Dec 29, 2024 · 0 comments
Labels
check-request Request for a new check in clang-tidy clang-tidy

Comments

@denzor200
Copy link

Assume we have a vector of 4 integers:

std::vector<int> v = {1,2,3,4};

And then imagine we need to make a copy of that vector:

auto v2 = std::vector{v.begin(), v.end()};

Are you expecting v2 to be a vector of 4 integers? Surprise :) This is a std::vector<std::vector<int>::iterator> with 2 iterators.
The way to fix that copying:

auto v2 = std::vector(v.begin(), v.end());

Note that it's easy to reach that typo due to code styles which are forcing programmers to always use auto and always prefer braced initialization.
You may suggest to change copying to auto v2 = v;, that's correct but not always possible - imagine we are copying subarray.

@EugeneZelenko EugeneZelenko added the check-request Request for a new check in clang-tidy label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
check-request Request for a new check in clang-tidy clang-tidy
Projects
None yet
Development

No branches or pull requests

2 participants