nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
938 stars 134 forks source link

Unit literal constructed from integral 0 passed to to_string causes std::out_of_range #266

Closed ts826848 closed 3 years ago

ts826848 commented 3 years ago

Passing a unit created using using a unit literal and 0 (e.g., 0_psi) to to_string causes std::out_of_range to be thrown:

awang@rp units % cat test.cpp
#include <units/pressure.h>

int main() {
    using namespace units::literals;
    auto s = to_string(0_psi);
}
awang@rp units % /usr/local/opt/llvm/bin/clang++ -I include/ -std=c++17 -g test.cpp
awang@rp units % ./a.out
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
zsh: abort      ./a.out
awang@rp units % /usr/local/opt/gcc/bin/g++-10 -I include/ -std=c++17 -g test.cpp
awang@rp units % ./a.out
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::erase: __pos (which is 18446744073709551615) > this->size() (which is 1)
zsh: abort      ./a.out
awang@rp units % /usr/local/opt/llvm/bin/clang++ --version
clang version 11.0.0
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
awang@rp units % /usr/local/opt/gcc/bin/g++-10 --version
g++-10 (Homebrew GCC 10.2.0) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

An exception is also thrown for 00_psi, 000_psi, etc. Nothing bad appears to happen with with non-zero values or with 0.0_psi.

This is using the latest version of the v3.x branch (791dab538f5dc890b031edac7e2a9b21d7436bf5).

ts826848 commented 3 years ago

Looks like this was fixed by #274