加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何在不使用额外模板参数的情况下使用模板模板参数声明/定

发布时间:2020-12-16 10:38:08 所属栏目:百科 来源:网络整理
导读:考虑以下模板模板参数的使用…… #include iostreamtemplate typename Xclass A{ X _t;public: A(X t) :_t(t) { } X GetValue() { return _t; }};template typename T,template typename T class C class B{ CT _c;public: B(T t) :_c(t) { } T GetValue() {
考虑以下模板模板参数的使用……

#include <iostream>

template <typename X>
class A
{
    X _t;
public:
    A(X t)
        :_t(t)
    {
    }
    X GetValue()
    {
        return _t;
    }
};

template <typename T,template <typename T> class C >
class B
{
    C<T> _c;
public:
    B(T t)
        :_c(t)
    {
    }
    T GetValue()
    {
        return _c.GetValue();
    }
};

using namespace std;

int main()
{
    B<int,A> b(10);
    cout<<b.GetValue();
    return 0;
}

有没有办法可以删除模板参数T?例如,是否有办法进行以下工作?

//Does not compile
template <template <typename T> class C >
class B
{
    C _c;
public:
    B(T t)
        :_c(t)
    {
    }
    T GetValue()
    {
        return _c.GetValue();
    }
};

int main()
{
    B< A<int> > b(10);
    cout<<b.GetValue();
    return 0;
}

解决方法

我假设您在代码中使用X和A之后.

通常的模式是

template<typename C>
struct B
{
   C c;
};

然后,在符合替代条件的内部课程中:

template<typename X>
class A
{
   typedef X type_name;
   X t;
};

然后,您可以使用C :: type_name访问模板参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读