ここで説明されているように、型がTYPEパラメーターを使用するクラス テンプレートのインスタンス化であるかどうかを確認するのは簡単なことです。
しかし...任意の数の異種の NON-TYPEパラメータを持つテンプレートを受け入れる可変長の「is_instantiation_of__ntp<...>」(Non-Type Paramsを表すNTP)を持つことは可能ですか? 例えば:
template<char*, bool, long, size_t, char>
struct many_hetero_nontype_params_example {};
char HELLO_WORLD[] = "hello world";
using ManyHeteroNontypeParamsEx = many_hetero_nontype_params_example<HELLO_WORLD, false, -16, 777, 'x'>;
そして、次のように使用できるようにします。
is_instantiation_of__ntp<char*, bool, long, size_t, char, many_hetero_nontype_params_example, ManyHeteroNontypeParamsEx>::value
これは、1) 「同種」(同じ型の値)、または 2) 少数のパラメーター (非変数ソリューションが実用的であるように) である NON-TYPE パラメーター リストに対して簡単に実装できることを知っています。私が話していることをよりよく理解するために、これらの特殊なケースを示すテスト ケース (gcc 4.7.0 でコンパイル) を書きました。
namespace test__is_instantiation_of__
{
// is_instantiation_of
template< template<typename...> class Template, typename T >
struct is_instantiation_of : std::false_type {};
template< template<typename...> class Template, typename... Args >
struct is_instantiation_of< Template, Template<Args...> > : std::true_type {};
// is_instantiation_of__homogeneous_nontype_params__
template< typename NTP, template<NTP...> class Template, typename T >
struct is_instantiation_of__homogeneous_nontype_params__ : std::false_type {};
template< typename NTP, template<NTP...> class Template, NTP... Args >
struct is_instantiation_of__homogeneous_nontype_params__< NTP, Template, Template<Args...> > : std::true_type {};
// is_instantiation_of__fixedcount_nontype_params__
template< typename NTP1, typename NTP2, template<NTP1, NTP2> class Template, typename T >
struct is_instantiation_of__fixedcount_nontype_params__ : std::false_type {};
template< typename NTP1, typename NTP2, template<NTP1, NTP2> class Template, NTP1 v1, NTP2 v2 >
struct is_instantiation_of__fixedcount_nontype_params__< NTP1, NTP2, Template, Template<v1, v2> > : std::true_type {};
// type_params_example
template<typename T1, typename T2, typename T3>
struct type_params_example {};
// homogeneous_nontype_params_example
template<bool B1, bool B2, bool B3, bool B4>
struct homogeneous_nontype_params_example {};
// fixedcount_nontype_params_example
template<long L, char C>
struct fixedcount_nontype_params_example {};
using /*.........*/ TypeParamsEx = /*..........*/ type_params_example<std::string, std::tuple<long, void*>, double>;
using HomogenousNontypeParamsEx = homogeneous_nontype_params_example<true, false, true, false>;
using FixedCountNontypeParamsEx = fixedcount_nontype_params_example<777, 'x'>;
void run()
{
using std::cout;
using std::endl;
if ( is_instantiation_of<type_params_example, TypeParamsEx>::value ) {
cout << "[TypeParamsEx] specializes [type_params_example]" << endl;
}
if ( is_instantiation_of<type_params_example, HomogenousNontypeParamsEx>::value ) {
cout << "[HomogenousNontypeParamsEx] specializes [type_params_example]" << endl;
}
if ( is_instantiation_of<type_params_example, FixedCountNontypeParamsEx>::value ) {
cout << "[FixedCountNontypeParamsEx] specializes [type_params_example]" << endl;
}
if ( is_instantiation_of__homogeneous_nontype_params__<bool, homogeneous_nontype_params_example, TypeParamsEx>::value ) {
cout << "[TypeParamsEx] specializes [homogeneous_nontype_params_example]" << endl;
}
if ( is_instantiation_of__homogeneous_nontype_params__<bool, homogeneous_nontype_params_example, HomogenousNontypeParamsEx>::value ) {
cout << "[HomogenousNontypeParamsEx] specializes [homogeneous_nontype_params_example]" << endl;
}
if ( is_instantiation_of__homogeneous_nontype_params__<bool, homogeneous_nontype_params_example, FixedCountNontypeParamsEx>::value ) {
cout << "[FixedCountNontypeParamsEx] specializes [homogeneous_nontype_params_example]" << endl;
}
if ( is_instantiation_of__fixedcount_nontype_params__<long, char, fixedcount_nontype_params_example, TypeParamsEx>::value ) {
cout << "[TypeParamsEx] specializes [fixedcount_nontype_params_example]" << endl;
}
if ( is_instantiation_of__fixedcount_nontype_params__<long, char, fixedcount_nontype_params_example, HomogenousNontypeParamsEx>::value ) {
cout << "[HomogenousNontypeParamsEx] specializes [fixedcount_nontype_params_example]" << endl;
}
if ( is_instantiation_of__fixedcount_nontype_params__<long, char, fixedcount_nontype_params_example, FixedCountNontypeParamsEx>::value ) {
cout << "[FixedCountNontypeParamsEx] specializes [fixedcount_nontype_params_example]" << endl;
}
}
}
予想どおり、得られる出力は次のとおりです。
[TypeParamsEx] specializes [type_params_example]
[HomeogenousNonTypeParamsEx] specializes [homogeneous_nontype_params_example]
[FixedCountNonTypeParamsEx] specializes [fixedcount_nontype_params_example]
問題は、上記の many_hetero_nontype_params_example でこれらのテンプレートが機能しないことです。つまり、任意の数の異種の非型パラメーターを持つテンプレートを受け入れる、単一の可変長の「is_instantiation_of__ntp」。
プライマリ テンプレートがテンプレート パラメーター リストの最後にパラメーター パックを持つ必要がなければ、これは簡単に実装できると思います。または、wrapper-struct/nested-struct アプローチを使用できる場合。これが私の(失敗した)試みです:
namespace test__is_instantiation_of__nontypes__
{
template<char*, bool, long, size_t, char>
struct many_hetero_nontype_params_example {};
char HELLO_WORLD[] = "hello world";
using ManyHeteroNontypeParamsEx = many_hetero_nontype_params_example<HELLO_WORLD, false, -16, 777, 'x'>;
/*
* is_instantiation_of__nontypes_v1__ (version 1)
* if uncommented, syntax error as expected ...
* error: parameter pack 'NTPs' must be at the end of the template parameter list
* error: parameter pack argument 'NTPs ...' must be at the end of the template argument list
*/
//template< typename... NTPs, template<NTPs...> class Template, typename T >
//struct is_instantiation_of__nontypes_v1__ : std::true_type {};
//template< typename... NTPs, template<NTPs...> class Template, NTPs... NonTypeArgs >
//struct is_instantiation_of__nontypes_v1__< NTPs..., Template, Template<NonTypeArgs...> > : std::true_type {};
/*
* is_instantiation_of__nontypes_v2__ (version 2)
* no syntax error (but see instantiation errors below)
*/
template<typename... NTPs>
struct is_instantiation_of__nontypes_v2__
{
template< template<NTPs...> class Template, typename T >
struct impl : std::false_type {};
template< template<NTPs...> class Template, NTPs... NonTypeArgs >
struct impl< Template, Template<NonTypeArgs...> > : std::true_type {};
};
void run()
{
/*
* uncommented since "v1" template has syntax error, but this is how it would be used ...
*/
//if ( is_instantiation_of__nontypes_v1__<char*, bool, long, size_t, char, many_hetero_nontype_params_example, ManyHeteroNontypeParamsEx>::value ) {
// std::cout << "yes" << std::endl;
//}
/*
* "v2" template has no syntax error, but the following attempt to use it results in these errors ...
*
* error: type/value mismatch at argument 1 in template parameter list for 'template<class ... NTPs> template<template<template<NTPs ...<anonymous> > class Template, class T> template<class ... NTPs> template<NTPs ...<anonymous> > class Template, class T> struct is_instantiation_of__nontypes_v2__<NTPs>::impl'
* error: expected a template of type 'template<class ... NTPs> template<NTPs ...<anonymous> > class Template', got 'template<char* <anonymous>, bool <anonymous>, long int <anonymous>, long unsigned int <anonymous>, char <anonymous> > struct many_hetero_nontype_params_example'
*/
//if ( is_instantiation_of__nontypes_v2__<char*, bool, long, size_t, char>::impl<many_hetero_nontype_params_example, ManyHeteroNontypeParamsEx>::value ) {
// std::cout << "yes" << std::endl;
//}
}
}
...
これに対する可変的な解決策は可能ですか?
前もって感謝します。