[cfe-dev] An optical illusion

Csaba Raduly via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 7 23:52:54 PST 2018


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)



More information about the cfe-dev mailing list