An optical illusion

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

An optical illusion

Hans Wennborg via cfe-dev
Hi all,

Simon Brand's "How C++ Debuggers Work" presentation
(https://www.youtube.com/watch?v=Q3Rm95Mk03c) had the following
snippet:


#include <unordered_set>
#include <string>
#include <iostream>

int main() {
  std::unordered_set<std::string> set
  {{"hello", "stop-the-"}};
  for (auto x : set) {
    std::cout << (x + " world") << '\n';
  }
}

(this is a set with one string, but on Linux this fails with a
std::length_error being thrown)

I believe calling the std::string::string(Iterator, Iterator)
constructor with string literals is always wrong, because two string
literals can not define a valid range.

Which tool should be able to warn about this? A compiler warning seems
wrong, because the compiler can't know what the constructor does with
its arguments. Perhaps a job for the static analyzer?


Csaba
--
You can get very substantial performance improvements by not doing the
right thing.
   - Scott Meyers, An Effective C++11/14 Sampler
So if you're looking for a completely portable, 100% standards-conformat way
to get the wrong information: this is what you want. - Scott Meyers (C++TDaWYK)
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: An optical illusion

Hans Wennborg via cfe-dev


On Thu, Mar 8, 2018 at 8:53 AM Csaba Raduly via cfe-dev <[hidden email]> wrote:
Hi all,

Simon Brand's "How C++ Debuggers Work" presentation
(https://www.youtube.com/watch?v=Q3Rm95Mk03c) had the following
snippet:


#include <unordered_set>
#include <string>
#include <iostream>

int main() {
  std::unordered_set<std::string> set
  {{"hello", "stop-the-"}};
  for (auto x : set) {
    std::cout << (x + " world") << '\n';
  }
}

(this is a set with one string, but on Linux this fails with a
std::length_error being thrown)

I believe calling the std::string::string(Iterator, Iterator)
constructor with string literals is always wrong, because two string
literals can not define a valid range.

Which tool should be able to warn about this? A compiler warning seems
wrong, because the compiler can't know what the constructor does with
its arguments. Perhaps a job for the static analyzer?

clang-tidy seems like a good fit.
 


Csaba
--
You can get very substantial performance improvements by not doing the
right thing.
   - Scott Meyers, An Effective C++11/14 Sampler
So if you're looking for a completely portable, 100% standards-conformat way
to get the wrong information: this is what you want. - Scott Meyers (C++TDaWYK)
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev