C++和CRTP模式的实现与编译器困境
C++ and CRTP pattern implementation and compiler dilemma
我正在尝试编译以下代码,但似乎有一个问题我无法解决:
template <int x>
struct count_x
{
enum { x_size = x };
};
template <typename y>
struct crtp_base
{
typedef typename y::count_t count_t;
crtp_base(const count_t&){}
};
template <int x>
struct derived : public crtp_base<derived<x> >
{
typedef typename count_x<x> count_t;
typedef crtp_base<derived<x> > base_t;
derived(const count_t& c) : base_t(c){}
};
int main()
{
derived<2> d((count_x<2>()));
return 0;
}
当使用clang 3.1编译时,以下是错误:
c:clangllvmcodeexample.cc:18:21: error: expected a qualified name after 'typename'
typedef typename count_x<x> count_t;
^
c:clangllvmcodeexample.cc:18:21: error: typedef name must be an identifier
typedef typename count_x<x> count_t;
^~~~~~~~~~
c:clangllvmcodeexample.cc:18:28: error: expected ';' at end of declaration list
typedef typename count_x<x> count_t;
^
;
c:clangllvmcodeexample.cc:20:18: error: no template named 'count_t'; did you mean 'count_x'?
derived(const count_t& c)
^~~~~~~
count_x
c:clangllvmcodeexample.cc:2:8: note: 'count_x' declared here
struct count_x
^
c:clangllvmcodeexample.cc:20:18: error: use of class template count_x requires template arguments
derived(const count_t& c)
^
c:clangllvmcodeexample.cc:2:8: note: template is declared here
struct count_x
^
5 errors generated.
我相信这与模板在编译时的确定方式有关,也与它们是否在正确的时间被确定为类型有关。我还尝试添加"using base_t::count_t;",但没有成功。除此之外,编译器产生的诊断让我真的很失落。如果您能就这一错误提供答案或建议,我们将不胜感激。
count_x<x>
不是限定名称(它根本没有::
!),因此不能在它前面加上typename
。
一旦修复了这个问题,代码仍然会失败,因为编译器在实例化CRTP基时还没有看到派生类型的嵌套typedef。另一个问题显示了一些替代方案。
相关文章:
- C/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我需要知道编译器如何在cpp中使用析构函数
- 编译器如何区分std::vector的构造函数
- CLANG 编译器 说:变量"PTR"可能未初始化
- 告诉c++编译器该参数没有别名
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么所有C++编译器都会崩溃或挂起此代码
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- C++和CRTP模式的实现与编译器困境
- 将这个实例变量添加到C++11文件的头中会使编译器陷入困境.为什么?