में स्थित खाके की एक टेम्पलेट उर्फ का उपयोग करने के बजाय एक पिछले प्रश्न से:एक टेम्पलेट
Doing a static_assert that a template type is another template
एंडी छिपकर मुझे इस कोड है कि मुझे static_assert
करने की अनुमति देता है कि एक टेम्पलेट प्रकार अन्य टेम्पलेट प्रकार है के साथ प्रदान की:
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of : public std::false_type { };
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : public std::true_type { };
template<typename T>
struct foo {};
template<typename FooType>
struct bar {
static_assert(is_instantiation_of<foo,FooType>::value, ""); //success
};
int main(int,char**)
{
bar<foo<int>> b; //success
return 0;
}
यह बहुत अच्छा काम करता है।
लेकिन अगर मैं foo
का एक अन्य नाम का उपयोग करने के लिए इस तरह कोड बदलने के लिए, कुछ बुरा जाना:
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of : public std::false_type { };
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : public std::true_type { };
template<typename T>
struct foo {};
//Added: alias for foo
template<typename T>
using foo_alt = foo<T>;
template<typename FooType>
struct bar {
//Changed: want to use foo_alt instead of foo here
static_assert(is_instantiation_of<foo_alt,FooType>::value, ""); //fail
};
int main(int,char**) {
//both of these fail:
bar<foo<int>> b;
bar<foo_alt<int>> b2;
return 0;
}
इस हल किया जा सकता है?
हम्म, ऐसा लगता है कि 'foo_alt' एक * typedef-name * है और एक * टेम्पलेट-नाम * नहीं है ... लेकिन यह केवल आपके' बार' को प्रभावित करता है; आप 'मुख्य' के रूप में इसे रख सकते हैं। –
मुझे लगता है कि आप जांच सकते हैं कि दो प्रकार एक ही टेम्पलेट के तत्काल हैं, यह उपनाम टेम्पलेट्स के साथ भी काम करता है। – dyp