c++ - 非类型模板参数的类型抽象

标签 c++ templates

我想编写一个模板,该模板可以将类型解构为具有非类型模板参数及其非类型模板参数的模板。例如,它将Array<5>解构为template<int> Array5,但通常可用于任何类型的非类型模板参数(整数类型,指针,成员指针等)。

首先尝试使用模板特化:

template<typename T> struct foo { enum { n = 1 }; };

template<int x> struct bar { enum { n = x }; };

template<typename T, template<T> class X, T x>
struct foo< X<x> > { enum { n = x }; }; // here x must be of integral type, but that's just for testing

int main(int, char**) { return foo< bar<16> >::n; }

Clang 3.1说:
test145.cpp:6:8: warning: class template partial specialization contains a template parameter that can not be deduced; this partial specialization will never be used
struct foo< X<x> > { enum { n = x }; };
       ^~~~~~~~~~~
test145.cpp:5:19: note: non-deducible template parameter 'T'                     
template<typename T, template<T> class X, T x>
                  ^
1 warning generated.

第二次尝试,使用功能模板:
template<typename T, T x> 
struct box 
{ 
    static constexpr T value() { return x; }
};

template<typename T, template<T> class X, T x>
box<T, x> foo(X<x>);

template<int> struct asdf { };

int main(int, char**) { return decltype(foo(*(asdf<9>*)0))::value(); }

lang语:
test150.cpp:12:41: error: no matching function for call to 'foo'
int main(int, char**) { return decltype(foo(*(asdf<9>*)0))::value(); }
                                        ^~~
test150.cpp:8:11: note: candidate template ignored: couldn't infer template argument 'T'
box<T, x> foo(X<x>);
          ^
1 error generated.

GCC 4.7说了类似的话。

这是基本限制吗?

额外的问题:如果是这样,那么,即使它不是那么简单和通用的代码,也有什么办法可以处理有限数量的代码中的所有无限可能性? (例如,使用指针会变得很困难:出于同样的原因,您似乎无法编写template<T>,我也不认为您也可以编写template<T*>。)

请不要问我为什么问。

最佳答案

另一个问题是询问基本相同的问题,只是要求模板类型参数,而不是模板非类型参数:template metaprogramming: (trait for?) dissecting a specified template into types T<T2,T3 N,T4, ...>

对于类型参数,这确实很容易。代码如下:

#include <tuple>
#include <vector>

template <class T> struct explode;

template <template <class... Args> class T, class... N>
struct explode<T<N...>>
{
    typedef T<N...> type;
    template <class... Args> using template_ = T<Args...>;
    template <int I> using type_parameter =
        typename std::tuple_element<I, std::tuple<N...>>::type;
};

#if TESTING
void test_harness()
{
    typedef explode<std::vector<int>> exv;

    exv::template_<char> vchar;  // The second parameter still has its default argument!
    exv::template_<exv::type_parameter<0>, exv::type_parameter<1>> vint;

    static_assert(std::is_same<exv::template_<char>, std::vector<char>>::value, "");
    static_assert(std::is_same<decltype(vchar), std::vector<char>>::value, "");
    static_assert(std::is_same<decltype(vint), std::vector<int>>::value, "");
    static_assert(std::is_same<exv::type, std::vector<int>>::value, "");
    static_assert(std::is_same<exv::type_parameter<0>, int>::value, "");
    static_assert(std::is_same<exv::type_parameter<1>, std::allocator<int>>::value, "");
}
#endif

但是对于非类型参数,我还没有弄清楚是否可行。您可以从外观类似的代码开始
template <class... ArgTypes, template <ArgTypes... Args> class T, ArgTypes... N>
struct explode<T<N...>>
{
    typedef T<N...> type;
    template <ArgTypes... Args> using template_ = T<Args...>;
    template <int I> using type_of_parameter =
        typename std::tuple_element<I, std::tuple<ArgTypes...>>::type;
    template <int I> struct nontype_parameter {
        static constexpr type_of_parameter<I> value() {
            return std::get<I>(std::tuple<ArgTypes...>(N...));
        }
    };
};

};

但是Clang(至少)不接受它:
test.cc:8:8: warning: class template partial specialization contains a template
      parameter that can not be deduced; this partial specialization will never
      be used
struct explode<T<N...>>
       ^~~~~~~~~~~~~~~~
test.cc:7:20: note: non-deducible template parameter 'ArgTypes'
template <class... ArgTypes, template <ArgTypes... Args> class T, ArgTypes... N>
                   ^

即使您以某种方式解决了该问题,您仍然必须用手工编码的std::get版本替换constexpr,因为无论出于何种原因,标准库的std::get都不是constexpr

关于c++ - 非类型模板参数的类型抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759952/

相关文章:

c++ - 为什么模板只能在头文件中实现?

c++ - 如何标记字符串

c++ - 比较数组中的随机值

c++ - 不确定如何使用模板调用类函数

c++ - 返回推力二值函数

c++ - 有没有办法决定模板类的模板参数类型?

C++ `long long` 变量类型

c# - 一般 : Programming Code Guidelines & Styles

html - 如何在 Vaadin 中使用 HTML 模板?

c++ - 如何编写代码在 C++ 中进行条件模板实例化