【发布时间】:2019-05-04 18:36:34
【问题描述】:
在以下代码中(https://wandbox.org/permlink/rA7lnXM6eQR4JhSM)
#include <type_traits>
template <typename T>
struct Identity : public T {};
class Something {
public:
Something() = default;
Something(const Something&) = delete;
Something(Something&&) = default;
Something& operator=(const Something&) = default;
Something& operator=(Something&&) = default;
template <
typename T,
typename U = std::decay_t<T>,
std::enable_if_t<Identity<
std::is_constructible<U, T&&>>::value>* = nullptr>
explicit Something(T&&) {};
};
int main() {
static_cast<void>(std::is_constructible<Something, const Something&>{});
}
我收到以下错误
error: base class has incomplete type
struct Identity : public T {};
~~~~~~~^
当我在此 (https://wandbox.org/permlink/MFJCHUzeKnS4yR0d) 的约束中删除带有 Identity 的间接时,错误消失了
template <
typename T,
typename U = std::decay_t<T>,
std::enable_if_t<
std::is_constructible<U, T&&>::value>* = nullptr>
explicit Something(T&&) {};
据我了解,这里的问题是我们试图实例化std::is_constructible,然后实例化Something 的构造函数,然后再实例化std::is_constructible,依此类推。
但是当我尝试在没有Identity 的情况下编译它时,为什么错误会消失?为什么我使用Identity时会出错?
【问题讨论】:
-
is_constructible_v从 C++17 开始,只是为了简化代码 -
在我脑海中浮现:
std::is_constructible不继承自它的任何一个参数,因此它的操作约束与您的 Identity 类不同。
标签: c++ templates language-lawyer c++17 sfinae