为依赖参数模板建立朋友

时间:2013-09-06 20:39:39

标签: c++ templates

这是一个错误还是我对模板一无所知?如果是这样,为什么它会像这样结束,可以做些什么呢?

如果我删除前两个前向声明,我会在BARef定义中得到相同的错误。

template<typename T, 
         unsigned int blockInitSize, 
         unsigned int blockMaxSize>
struct BAInternalIndex;

template<typename T,
         unsigned int blockInitSize, 
         unsigned int blockMaxSize,
         BAInternalIndex<T, blockInitSize, blockMaxSize>* (&getIndexPointer) (const T&)>
class BARef;

template<typename T, 
         unsigned int blockInitSize, 
         unsigned int blockMaxSize>
struct BAInternalIndex
{
    template<typename T2, 
             unsigned int blockInitSize2, 
             unsigned int blockMaxSize2,
             BAInternalIndex<T2, blockInitSize2, blockMaxSize2>* (&getIndexPointer2) (const T2&)>
    friend class BARef;
};

error C3855: 'BARef': template parameter 'getIndexPointer' is incompatible with the declaration

尝试使用Visual C ++ 9.0和11.0进行编译。


最小例子/ SSC(n)CE:

template<typename T>
struct A;

template<typename T, void(&fun)(A<T>)>
class B;

template<typename T>
struct A
{
    template<typename T2, void(&fun)(A<T2>)>
    friend class B;                                // line 11
};                                                 // line 12

int main()
{
    A<int> a;                                      // line 16
}

VS2010(v10)错误输出:

(line 11) error C3855: `B`: template parameter `fun` is incompatible with the
                            declaration
(line 12)              see reference to class template instantiation `A` being
                       compiled

(line 11) error C3855: `B`: template parameter `fun` is incompatible with the
                            declaration
(line 16)              see reference to class template instantiation `A` being
                       compiled
                       with
                       [
                           `T=int`
                       ]

0 个答案:

没有答案