简体   繁体   English

CRTP的C ++编译器错误

[英]C++ Compiler error with CRTP

I have the following class hierarchy: 我有以下类层次结构:

template <typename T>
class base
{
public:
   void f() {}
};

class class_a : public base<class_a> {};

class class_b : public base<class_b>, 
                public class_a 
{ 
   using base<class_b>::f; 
};

int main()
{
   class_b b;
   b.f();
   return 0;
}

Comeu and Intel C++ v11 claim all is well, however GCC (4.4.1) and VC++ 2008 seem to complain ( http://codepad.org/KQPDsqSp ), eg: Comeu和英特尔C ++ v11声称一切都很好,但GCC(4.4.1)和VC ++ 2008似乎抱怨( http://codepad.org/KQPDsqSp ),例如:

g++ -pedantic -Wall -o test test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:5: error: ‘void base<T>::f() [with T = class_b]’ is inaccessible
test.cpp:14: error: within this context 

I believe the code is well formed as it is, however I could be wrong, I'm hoping someone from the SO C++ community could provide some insight into this issue. 我相信代码很好,但是我可能错了,我希望来自SO C ++社区的人能够对这个问题提供一些见解。

Note: Adding "public" before the using directive in class_b, resolves the issue for both gcc and VS. 注意:在class_b中的using指令之前添加“public”可以解决gcc和VS的问题。 Should the accessor section of the class in which the using directive is applied override the derivation mode (public, private) of the base class? 应用using指令的类的访问器部分是否应该覆盖基类的派生模式(public,private)?

In short is this 简而言之就是这样

  • A compiler error - if so which compiler GCC,VS or Comeu,Intel 编译器错误 - 如果是这样,编译器GCC,VS或Comeu,Intel
  • Is the above code well formed? 以上代码是否形成良好?
  • Does the accessor section in which a using directive is called override the derivation mode of the base? 调用using指令的访问器部分是否会覆盖基础的派生模式?

What you are doing here, is resolving an ambiguity by importing the symbol into the classes private namespace. 你在这里做的是通过将符号导入类私有命名空间来解决歧义。 Hence it's method shadowing and changing it's visibility to private. 因此,它的方法是阴影并改变它对私人的可见性。 You can't have two functions with the exact same prototype both private and public, hence the f is now private. 你不能拥有两个完全相同的私有和公共原型的函数,因此f现在是私有的。

At least GCC believes that using should be able to change the visibility of a function. 至少GCC认为使用应该能够改变功能的可见性。

Vague references however found in GCC bug database , show that using in fact shouldn't be affected by scope. 然而,在GCC bug数据库中发现的模糊引用表明,实际上使用不应受范围的影响。

Most importantly, a direct answer (C++ Standard '03 -- 7.3.3/15) 最重要的是,直接回答(C ++标准'03 - 7.3.3 / 15)

The alias created by the using-declaration has the usual accessibility for a member-declaration. using-declaration创建的别名具有成员声明的通常可访问性。

Hence the answers would be: 因此答案是:

  • it's a bug in Comeau 这是Comeau中的一个错误
  • no, the code is not well formed, at least C++03-wise (can't find anything related in C++0x N3000) 不,代码没有很好地形成,至少是C ++ 03智能(在C ++ 0x N3000中找不到任何相关内容)
  • yes, you can change access scope 是的,您可以更改访问范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM