के लिए सी ++ 17 अद्यतन: सी ++ 17 के गुना भाव के साथ इस लगभग तुच्छ हो जाता है:
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert(std::is_base_of_v<Type, Requirements> && ..., "Invalid.");
};
मूल उत्तर (सी ++ 11/14): आप पैक विस्तार का उपयोग कर सकते हैं और std::all_of
के कुछ स्थिर संस्करण का उपयोग कर सकते हैं:
template <bool... b> struct static_all_of;
//implementation: recurse, if the first argument is true
template <bool... tail>
struct static_all_of<true, tail...> : static_all_of<tail...> {};
//end recursion if first argument is false -
template <bool... tail>
struct static_all_of<false, tail...> : std::false_type {};
// - or if no more arguments
template <> struct static_all_of<> : std::true_type {};
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert(static_all_of<std::is_base_of<Type, Requirements>::value...>::value, "Invalid.");
// pack expansion: ^^^
};
struct Base {};
struct Derived1 : Base {};
struct Derived2 : Base {};
struct NotDerived {};
int main()
{
CommonBase <Base, Derived1, Derived2> ok;
CommonBase <Base, Derived1, NotDerived, Derived2> error;
}
पैक विस्तार मूल्यों आप std::is_base_of<Type, ?>::value
में प्रश्न चिह्न, मुख्य में पहली पंक्ति के लिए अर्थात के लिए Requirements...
में हर प्रकार के डालने से प्राप्त की सूची में विस्तार होगा यह static_all_of<true, true>
करने का विस्तार होगा, दूसरी पंक्ति के लिए यह हो जाएगा static_all_of<true, false, true>
स्रोत
2012-11-26 10:57:13
मुझे शक है, तो यह संभव है। इसके अलावा आपकी पहली 'is_base_of <आवश्यकताएँ ...> :: value' में दूसरे तर्क का उल्लेख नहीं है। – iammilind
'static_assertion' समय प्रक्रिया संकलित है ([यहां] (http://stackoverflow.com/questions/1647895/what-does-static-assert-do-and-what-would-you-use-it-for)) संकलन समय पर कंपाइलर चेक 'is_base_of' मान करें? –
@tAmirNaghizadeh निश्चित रूप से ऐसा करता है, क्योंकि 'is_base_of' एक टेम्पलेट स्वयं है, और टेम्पलेट संकलन समय पर तत्काल होते हैं। –