2

我使用的PC - 皮棉的专业化(静态代码分析伟大的工具 - 看到http://www.gimpel.com/) 对于下面的代码块:错误使用C++模板偏

class ASD { 
    protected: 
     template<int N> 
     void foo(); 
}; 

template<> 
inline void ASD::foo<1>() {} 

template<int N> 
inline void ASD::foo() {} 

PC-lint会给出我一个警告:

inline void ASD::foo<1>() {} 
mysqldatabaseupdate.h(7) : Error 1060: protected member 'ASD::foo(void)' is not accessible to non-member non-friend functions 

我相信代码是好的,误差在皮棉的一面,但我认为林特工具是真正伟大的工具,它更可能比我不知道的东西。那么这个代码好吗?

回答

1

的错误是在PC - 皮棉本身。它已在最新版本中修复。

2

您的struct ASD只有一个功能foo,它是在protected部分。它不能从非成员函数访问。同时struct ASD没有任何其他成员函数。所以没有人访问foo,我相信这是错误信息的原因。

尝试你的结构更改为以下,例如:

class ASD { 
    public: 
     void bar() { foo<1>(); } 
    protected: 
     template<int N> 
     void foo(); 
}; 
+1

但是不会有任意的子类可以访问'foo'函数吗? – 2010-08-18 14:03:31

+0

在这个例子中,我相信产生错误的代码中没有子类。 – 2010-08-18 14:15:23

+0

尝试向该类添加虚拟功能。它可能会在这里做一个权衡。 – 2010-08-18 18:21:23