【发布时间】:2019-08-31 22:38:58
【问题描述】:
当用户请求空值时,我想让std::optional 抛出异常。
#include <optional>
std::optional<int> oi;
int main(){
*oi; // Must throw
}
c++ -std=c++17 test.cc && ./a.out
正常工作。
在clang的libc++实现中我找到了。
_LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
value_type& value()
{
if (!this->__engaged_)
__throw_bad_optional_access();
return this->__val_;
}
_LIBCPP_INLINE_VISIBILITY
value_type&
operator*()
{
_LIBCPP_ASSERT(this->__engaged_, "optional operator* called for disengaged value");
return this->__val_;
}
c++ -std=c++2a -D_LIBCPP_DEBUG -D_LIBCPP_DEBUG_USE_EXCEPTIONS ./main.cc && ./a.out 输出
Undefined symbols for architecture x86_64:
"std::__1::__libcpp_db::__insert_c(void*)", referenced from:
void std::__1::__libcpp_db::__insert_c<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in main-eef9ea.o
"std::__1::__libcpp_db::swap(void*, void*)", referenced from:
...
如何将 libc++ 与调试符号链接?
附言
c++ --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
【问题讨论】:
-
顺便说一句,包装
std::optional并在执行所需的检查后正确抛出异常可能会更好。 -
我目前正在这样做。
-
为什么不坚持下去呢?而不是试图改变标准库特性的语义。这只会让人们感到困惑。
-
感谢有关“llvm libc++ DebugMode”的提示。我以为我们已经从文档中删除了它。问题是我们想为标记为
noexcept的东西添加断言,显然,你不能从这些东西中抛出。 -
更多:我们更新了文档;您正在查看 5.0 版本文档。当前版本是 8.0.1; 9.0 迫在眉睫;您正在查看的文档已过期。
标签: c++ macos clang llvm libc++