c ++ pimpl idiom:实现取决于模板参数 [英] c++ pimpl idiom : Implementation depending on a template parameter
问题描述
在此问题中,我未能成功询问如何使用不同的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)说明:
在一个明确的专门化中
类成员的声明
模板或
出现在命名空间范围中的成员模板,成员
模板和其包含的
类模板中的一些可以保持
未特殊化,除了
声明不应明确
专门化类成员模板如果
它的封闭类模板不是
显式专门化。在
这样的显式特化
声明中,将提供关键字模板
后跟模板参数列表
而不是明确的
模板< b $ b专业化声明的
成员。
模板参数列表中
模板参数的类型应为
,与主
模板定义中指定的相同。
[示例:
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:
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 ]
这篇关于c ++ pimpl idiom:实现取决于模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!