是否有一種可移植的方式來實作std::is_constructible使用概念而不STL使用requires運算式或模板元編程?
考慮這段代碼:
template <class T, class... Args>
struct is_constructible
: std::bool_constant<requires {
new T(std::declval<Args>()...);
}> {
};
它適用于除參考之外的其他資料型別,因為不能new作為參考型別。
// Test cases
auto main() -> int {
static_assert(is_constructible<int>::value);
static_assert(is_constructible<int, int>::value);
static_assert(is_constructible<int, float>::value);
static_assert(!is_constructible<int, int *>::value);
static_assert(is_constructible<int &, int &>::value);
static_assert(is_constructible<int &&, int &&>::value);
static_assert(!is_constructible<void, void>::value);
static_assert(!is_constructible<int(), int()>::value);
static_assert(is_constructible<int (*)(), int()>::value);
static_assert(!is_constructible<intptr_t, int *>::value);
static_assert(!is_constructible<int &, float &>::value);
static_assert(std::is_constructible<int>::value);
static_assert(std::is_constructible<int, int>::value);
static_assert(std::is_constructible<int, float>::value);
static_assert(!std::is_constructible<int, int *>::value);
static_assert(std::is_constructible<int &, int &>::value);
static_assert(std::is_constructible<int &&, int &&>::value);
static_assert(!std::is_constructible<void, void>::value);
static_assert(!std::is_constructible<int(), int()>::value);
static_assert(std::is_constructible<int (*)(), int()>::value);
static_assert(!std::is_constructible<intptr_t, int *>::value);
static_assert(!std::is_constructible<int &, float &>::value);
return {};
}
uj5u.com熱心網友回復:
不,當然不是“干凈利落”。
事實上,標準化程序中的早期提案試圖constructible_from使用requires運算式來實作,但是有太多的極端情況,我們放棄了,而是根據型別特征來指定它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/420594.html
標籤:
上一篇:如何在我的建構式中將shared_ptr初始化為QTextCodec?
下一篇:使用子變數而不重寫函式類
