【问题标题】:C++11: How to check if a type is an instantiation of a given class-template of "heterogeneous" NON-TYPE parameters?C ++ 11:如何检查类型是否是“异构”非类型参数的给定类模板的实例化?
【发布时间】:2013-03-02 14:57:34
【问题描述】:

我知道检查一个类型是否是采用 TYPE 参数的类模板的实例化很简单,如下所述:How can I check if a type is an instantiation of a given class template?

但是...是否有可能有一个可变参数“is_instantiation_of__ntp<...>”(NTP 代表非类型参数),它可以接受具有任意数量的 heterogeneous 非类型参数?例如:

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)少数参数(这样非可变参数解决方案是实用的)。我什至写了一个测试用例来演示这些特殊情况(使用 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”,它可以接受具有任意数量的异构非类型参数的模板。

我认为如果不要求主模板在模板参数列表的末尾有参数包,那么这将很容易实现。或者是否可以使用包装结构/嵌套结构方法。这是我的(失败的)尝试:

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;
        //}
    }
}

...

是否有可能为此提供可变参数解决方案?

提前致谢。

【问题讨论】:

  • 我想我在原来的答案中误解了你的问题,对不起。将其删除并提供更好的答案。
  • 您的原始答案解释了为什么没有明显的解决方案来解决这个问题,例如声明一个参数包“typename...ScalarTypes”然后声明模板模板“template class TheTemplate”在同一参数列表中。它不包括其他可能的解决方案,例如我发布的 is_instantiation_of__nontypes_v2__ 尝试,它使用封闭结构来声明嵌套结构的参数列表中声明的模板模板参数的参数包。
  • 如果您可以解释为什么“v2”尝试不起作用(特别是尝试使用它时的 gcc 错误),我会非常确信这是无法完成的,并且会接受答案。但是,很高兴知道。感谢您对此进行调查。
  • 这应该有助于澄清问题:如果您可以在 test__is_instantiation_of__nontypes__::run() 中获得“is_instantiation_of__nontypes_vN__”的任何一种用法(对于 many_hetero_nontype_params_example),它将回答这个问题。或者证明没有可能的解决方案。

标签: c++ templates c++11 variadic-templates template-meta-programming


【解决方案1】:

您可能遇到了编译器错误。我试图将其简化为一个更简单的示例:

#include <iostream>

template<bool, char> struct A { };

template<typename... Ts>
struct test
{  
    template<typename T>
    struct impl : std::false_type {};

    template<Ts... Args>
    struct impl<A<Args...>> : std::true_type {};
};

int main()
{
    using IA = A<false, 'x'>;
    std::cout << ((test<bool, char>::impl<IA>::value) ? "Y" : "N");
}

GCC 4.7.2 编译这个,但是编译的程序 prints the wrong output (N)。另一方面,Clang 3.2 做到了这一点,编译后的程序 prints the correct output (Y)。

这里是上述程序的略微修改版本,其中test 类模板非常类似于您的is_instantiation_of__nontypes_v2__ 类模板:

#include <iostream>

template<bool, char> struct A {};

template<typename... Ts>
struct test
{  
    template<template<Ts...> class TT, typename T>
    struct impl : std::false_type {};

    template<template<Ts...> class TT, Ts... Args>
    struct impl<TT, TT<Args...>> : std::true_type {};
};

int main()
{
    using IA = A<false, 'x'>;
    std::cout << ((test<bool, char>::impl<A, IA>::value) ? "Y" : "N");
}

当 Clang 编译这个并且编译的程序打印正确的输出 (Y) 时,GCC 发出以下编译错误:

需要'template&lt;class ... Ts&gt; template&lt;Ts ...&lt;anonymous&gt; &gt; class TT' 类型的模板,得到'template&lt;bool &lt;anonymous&gt;, char &lt;anonymous&gt; &gt; struct A'

看起来 GCC 不承认第一个模板模板参数应该有一个由 Ts 的扩展给出的模板参数列表。因此,在我看来这是 GCC 的一个错误。

【讨论】:

  • 更正了缺少“literal_type...”参数包,这使得这变得更加困难。但是,如果有某种方式在模板参数列表中声明“typename...ScalarTypes”,然后将模板模板定义为“template class TheTemplate”...问题是参数包必须在列表的末尾(在声明 TheTemplate 之后)。您可以在帖子末尾看到我的“失败的尝试”......我正在寻找一种真正有效的类似技术。我认为“is_instantiation_of__nontypes_v2__”很接近。
  • @etherice:我试图在最后一次编辑我的答案时对此发表评论。
  • 那条规则(你大胆的)适用于“v1”尝试,但它确实似乎适用于“v2”尝试。 “typename ... NTPs”在封闭结构的模板参数列表中声明,而不是嵌套结构。正如我所说,它确实可以编译,但是当我尝试使用它时会看到 gcc 错误。你能解释一下它产生的错误吗?
  • 我怀疑可能是这种情况,因为 gcc 的错误毫无意义,而且我不知道有任何规则会使其在语言方面非法或格式错误。感谢您确认它可以在其他编译器中使用。我将此 SO 问题标记为已回答,但我很好奇您是否对这是否是 gcc 中的已知错误以及是否已在较新版本中修复。
  • @etherice:我相信这还没有解决(GCC 4.8.0 beta 也有同样的问题)。也许你可以提交一个错误报告。
猜你喜欢
  • 2012-06-30
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
相关资源
最近更新 更多