我一直在研究 C++ 中的 std::constructible_from 概念,并注意到它的定义包括 destructible 和 is_constructible_v:
template<class _Tp, class... _Args>
concept constructible_from =
destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
Run Code Online (Sandbox Code Playgroud)
我明白为什么 is_constructible_v 是必要的,但我不清楚为什么还需要 destructible 。
我正在使用 Apple Clang 并在 C++ 中包含以下代码:
struct Foo {
int x;
Foo& operator=(const Foo&) = default;
};
struct Bar {
double x;
Bar& operator=(const Foo& foo) {
this->x = static_cast<double>(foo.x);
return *this;
}
};
int main(int argc, const char * argv[]) {
static_assert(std::assignable_from<Bar&, const Foo&>, "fail");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经定义了一个赋值运算符 inBar来分配 from Foo。但是,std::assignable_from<Bar&, const Foo&>仍然返回false,导致静态断言失败。我的理解是std::assignable_from<Bar&, const Foo&>应该返回true,因为由于我在 中定义的自定义赋值运算符,Bar可以从对象分配对象。FooBar
我正在用 Apple Clang 编译这个。这可能是编译器问题还是我误解了std::assignable_from工作原理?有人可以解释为什么std::assignable_from<Bar&, …