【发布时间】:2012-12-22 10:19:06
【问题描述】:
我编写了一个使用大量 C++11 元编程技术和 CRTP 的小型库,它可以很好地与 g++ 4.7.2 一起编译
现在,我尝试使用 Intel icpc 13.0.0.079 编译它,它会产生数百个错误。所以我尝试一个接一个地隔离问题。
所以,首先,考虑这段代码,它在 g++ 4.7.2 下编译没有问题
#include <iostream>
template<template<typename> class Crtp, typename Type>
struct Base {};
template<typename Type>
struct Derived : public Base<Derived, Type>
{
Derived(): Base<Derived, Type>() {;}
};
int main()
{
Derived<int> x;
return 0;
}
icpc 和 clang 都无法编译这段代码:
test_crtp.cpp(26): error: type "Derived<Type>::Derived" is not a class template
Derived(): Base<Derived, Type>() {;}
^
test_crtp.cpp(26): error: "Base" is not a nonstatic data member or base class of class "Derived<int>"
Derived(): Base<Derived, Type>() {;}
^
detected during instantiation of "Derived<Type>::Derived() [with Type=int]" at line 31
compilation aborted for test_crtp.cpp (code 2)
那么它是 intel 和 clang 还是 g++ 中的错误?如果是intel和clang,你觉得以后的版本会解决吗?
【问题讨论】:
-
这是一个很好的问题,但通常 CRTP 不会打扰模板模板参数,而只是让 Derived 类传递类型。
template<typename Derived> struct Base; template<typename T> struct Derived: Base< Derived<T> > { ... };比较正常。 -
...如果您想传播
Type,也许您可以在Derived中有一个typedef来暴露它... -
标识符
Derived在其自己的类中命名为完整类型Derived<Whatever>。这称为 injected 类名。我认为这是 GCC 的一个错误。 -
+ 好问题,我也想看到一个明确的答案
-
@Xeo :有没有办法在类中获取非完整类型?
标签: c++ templates c++11 compiler-errors crtp