c++ pimpl idiom :根据模板参数实现

发布于 2024-10-26 04:53:08 字数 1399 浏览 2 评论 0原文

这个问题中,我没有成功地询问如何使用不同的 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, 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;
}

如果我尝试专门化 struct C ,它仍然会失败(上面的内容无法编译)

那么,可以这样做吗?

我知道这样的解决方法:

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

但如果可能的话,我想避免它

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-11-02 04:53:08

你想要做的事情是语言不允许的。

§ 14.7.3.16 (FCD 2010-03-26) 规定:

明确的专业化
类成员的声明
模板或成员模板
出现在命名空间范围内的成员
模板及其一些附件
类模板可能会保留
非专业化,除了
声明不得明确
专门化一个类成员模板,如果
它的封闭类模板不是
也明确专门化。在
如此明确的专业化
声明,关键字模板
接下来是模板参数列表
应提供而不是
模板>>在明确的之前
专业化声明
成员。的类型
模板参数
模板参数列表应为
与初级中指定的相同
模板定义。

[ 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 ]

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

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

In an explicit specialization
declaration for a member of a class
template or a member template that
appears in namespace scope, the member
template and some of its enclosing
class templates may remain
unspecialized, except that the
declaration shall not explicitly
specialize a class member template if
its enclosing class templates are not
explicitly specialized as well. In
such explicit specialization
declaration, the keyword template
followed by a template-parameter-list
shall be provided instead of the
template<> preceding the explicit
specialization declaration of the
member. The types of the
template-parameters in the
template-parameter-list shall be the
same as those specified in the primary
template definition.

[ 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 ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文