C++ 类型特征提取模板参数类 [英] C++ type traits to extract template parameter class

查看:63
本文介绍了C++ 类型特征提取模板参数类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模板中,我想将模板参数向下钻取到真正的非模板化类型.所以:

template 结构我的模板{//遗憾的是没有extract_Basetypedef typename extract_base::MyType WorkType;};结构 X {};模板 结构模板{};//MyTemplate>::WorkType is X;//MyTemplate::WorkType is X;

我看到的唯一解决方案是定义真正的基类型,如 std::vector<X>::value_type 是 X.但我很好奇是否有一种方法可以做到这一点,而无需在每个目标模板中定义辅助类型.

我看到了类似 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2965.html 但这是草稿?我不太明白.

是的,我知道存在多重继承,但即使对于简单的情况,这也很好.

更新:Nawaz 解决方案非常适合我,并且很容易扩展到特定情况,例如

templateX类,类型名T1,类型名T2>struct extract_base <X<T1, T2>>//专业化{typedef T1 基;};

我什至可以将 is_base_of 或其他过滤器应用于 T1/T2 等等.所以它确实适用于 X<T,U>- 至少使用 g++ 4.6.7.

解决方案

首先,让我们称其为 value_type 而不是 base,因为 value_type 似乎更适合描述您要提取的类型.

你可以使用这个:

templatestruct extract_value_type//我们称之为extract_value_type{typedef T value_type;};模板<模板<类型名>X 类,类型名称 T>struct extract_value_type<X<T>>//专业化{typedef T value_type;};

只要 extract_value_type 的模板参数是 TX 的形式,它就应该可以工作.但是,它不适用于 X.但是使用可变参数模板在 C++11 中很容易实现它.

将其用作:

template 结构我的模板{typedef typename extract_value_type::value_type value_type;};

在线演示:http://ideone.com/mbyvj

<小时>

现在在 C++11 中,您可以使用可变参数模板使 extract_value_type 与接受多个模板参数的类模板一起使用,例如 std::vectorstd::setstd::list

template类 X,类型名称 T,类型名称 ...Args>struct extract_value_type//专业化{typedef T value_type;};

演示:http://ideone.com/SDEgq

In a template, I want to drill down the template parameter to the real non-templated type. So:

template <typename T>
struct MyTemplate
{
    // sadly there's no extract_Base
    typedef typename extract_base<T>::MyType WorkType;
};
struct X {};
template <typename T> struct Templ {};
//MyTemplate<Templ<X>>::WorkType is X;
//MyTemplate<X>::WorkType is X;

The only solution I see is to define real base type like std::vector<X>::value_type is X. But I am curious if there's a way to do this without defining auxiliary types inside each destination template.

I saw something like http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2965.html but this is draft? and I don't quite get it.

Yes I know there's multiple inheritance, but even for simple case this would be nice.

UPDATE: Nawaz solution works for me very well, and it is easy to extend to specific cases, e.g.

template<template<typename, typename> class X, typename T1, typename T2>
struct extract_base <X<T1, T2>>   //specialization
{
    typedef T1 base;
};

I can even apply is_base_of or other filters to T1/T2 and so on. So it does work for X<T,U> - at least with g++ 4.6.7.

解决方案

First of all, let's call it value_type instead of base, because value_type seems to be more appropriate term to describe the type which you want to extract.

You can use this:

template<typename T>
struct extract_value_type //lets call it extract_value_type
{
    typedef T value_type;
};

template<template<typename> class X, typename T>
struct extract_value_type<X<T>>   //specialization
{
    typedef T value_type;
};

It should work as long as the template argument to extract_value_type is of the form of either T or X<T>. It will not work for X<T,U>, however. But then it is easy to implement it in C++11 using variadic template.

Use it as:

template <typename T>
struct MyTemplate
{
    typedef typename extract_value_type<T>::value_type value_type;
};

Online demo : http://ideone.com/mbyvj


Now in C++11, you can use variadic template to make extract_value_type work with class templates which take more than one template arguments, such as std::vector, std::set, std::list etc.

template<template<typename, typename ...> class X, typename T, typename ...Args>
struct extract_value_type<X<T, Args...>>   //specialization
{
    typedef T value_type;
};

Demo : http://ideone.com/SDEgq

这篇关于C++ 类型特征提取模板参数类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆