- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
This page说make_optional
C++17 中的函数返回 constexpr optional<...>
.我认为(虽然我可能是错的)这需要 optional<T>
有一个 constexpr
复制或移动构造函数。然而,this page也说不是这样的。
我不知道如何make_optional
可以按照当前的 C++1z 草案实现。参见 this post为了澄清。是否有一些解决方法,或者这可能只是标准草案/cppreference 的错误?
最佳答案
感谢@Yakk 和@T.C.为了他们的解释。我觉得一个例子应该让事情更清楚:
struct wrapper {
int value;
// non-explicit constexpr constructor
constexpr wrapper(int v) noexcept : value(v) {}
// non-constexpr copy & move constructors
wrapper(const wrapper& that) noexcept : value(that.value) {}
wrapper(wrapper&& that) noexcept : value(that.value) {}
};
constexpr wrapper make_wrapper(int v)
{
return {v};
}
int main()
{
constexpr auto x = make_wrapper(123); // error! copy/move construction,
// but no constexpr copy/move ctor
constexpr int y = make_wrapper(123).value; // ok
static_assert(y == 123, ""); // passed
}
因此 make_wrapper
确实成功返回了一个 constexpr wrapper
;它是复制/移动构造(尽管通常被编译器省略)阻止代码编译,因为没有 constexpr
复制/移动构造函数。
我们可以通过使用其成员值初始化一个constexpr
变量来验证返回的(临时)wrapper
对象的constexpr
性。
关于C++17 make_optional constexpr-ness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933655/
我正在尝试 C++17 optional 类型,并认为使用它的合适位置是一个尝试打开文件并可能返回打开文件的函数。我写的函数是这样的: std::optional openFile(std::stri
所有std::make_随着 Class template argument deduction 的引入,C++17 变得多余(除了 make_unique 和 make_shared )。 那么 s
(可能不是 C++14,可能是 Library TS)工具 make_optional被定义(in n3672)为: template constexpr optional::type> mak
This page说make_optional C++17 中的函数返回 constexpr optional .我认为(虽然我可能是错的)这需要 optional有一个 constexpr复制或移动
我想更好地理解使用 default 构造函数创建 boost::optional 对象的区别: boost::optional pasta = boost::optional(spaghetti) 或
我最初的想法一直是尽可能尝试使用 make-helper 函数(也是因为它们与 almost-always-auto 概念和 east-initialization 约定巧妙地对齐),同时尝试在可以避
我是一名优秀的程序员,十分优秀!