本文介绍了c ++ pimpl idiom:实现取决于模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在中,我未能成功询问如何使用不同的pimpl



也许这个例子更好地说明我想要做什么:

  #include< iostream> 

template< int N,typename T>
struct B
{
B():c(new C N)
{}

template< int M>
struct C;
C< N> *C;
};

template< int N,类型名T>
template< int M>
struct B< N,T> :: C
{
int a [M];
};

//版本1不工作
template< int N,typename T>
template< >
struct B< N,T> 0>
{
int a;
};
//版本2不工作
template<类型名T>
template< int M>
struct B< 0,T> :: C
{
int a;
};


int main()
{
B< 0,float> b0;
B< 1,int> b1;

std :: cout<< b0 =< sizeof(b0.c-> a)< std :: endl;
std :: cout<< b1 =<< sizeof(b1.c-> a)< std :: endl;
}

如果我试图专门化struct C



我知道这样的工作:



/ p>

 模板< int M> 
struct D
{
int a [M];
};
template< >
struct D< 0>
{
int a;
};

template< int N,typename T>
template< int M>
struct B< N,T> :: C
{
D& M>帮手;
};

但如果可能,我想避免它



§14.7.3.16(FCD 2010)是一个非常有用的工具, -03-26)说明:



  [示例:
template< class T1>类A {
template< class T2>类B {
template< class T3> void mf1(T3);
void mf2();
};
};
模板<> template< class X>

class A< int> :: B {
template< class T> void mf1(T);
};
模板<>模板<> template< class T>
void A< int> :: B< double> :: mf1(T t){}
template< class Y>模板<>
void A< Y> :: B< double> :: mf2(){} // ill-formed; B双键。是专门的但
//它的封闭类模板A不是
-end example]


In this question I unsuccessfully asked how to use different pimpl implementation depending on a template argument.

Maybe this example ilustrates better what I am trying to do :

#include <iostream>

template< int N, typename T >
struct B
{
    B() : c( new C< N > )
    {}

    template< int M >
    struct C;
    C< N > *c;
};

template< int N, typename T >
template< int M >
struct B< N, T >::C
{
    int a[M];
};

// version 1 that doesn't work
    template< int N, typename T >
    template< >
    struct B< N, T >::C< 0 >
    {
        int a;
    };
// version 2 that doesn't work
    template< typename T >
    template< int M >
    struct B< 0, T >::C
    {
        int a;
    };


int main()
{
    B< 0, float >   b0;
    B< 1, int >     b1;

    std::cout << "b0 = " << sizeof(b0.c->a) << std::endl;
    std::cout << "b1 = " << sizeof(b1.c->a) << std::endl;
}

It still fails if I try to specialize the struct C (the above doesn't compile)

So, is it possible to do?

I know a work around like this :

template< int M >
struct D
{
  int a[M];
};
template<  >
struct D<0>
{
  int a;
};

template< int N, typename T >
template< int M >
struct B< N, T >::C
{
    D< M > helper;
};

but if possible, I would like to avoid it

解决方案

What you're trying to do is not allowed by the language.

§ 14.7.3.16 (FCD 2010-03-26) states:

[ Example:
template <class T1> class A {
    template<class T2> class B {
        template<class T3> void mf1(T3);
        void mf2();
    };
};
template <> template <class X>

class A<int>::B {
    template <class T> void mf1(T);
};
template <> template <> template<class T>
void A<int>::B<double>::mf1(T t) { }
template <class Y> template <>
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
// its enclosing class template A is not
—end example ]

这篇关于c ++ pimpl idiom:实现取决于模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:50