错误:"结束索引"中没有名为"rank_"的成员
error: no member named 'rank_' in 'EndIndex'
>我正在尝试使用自动微分库 Adept 并使其适用于 gcc 4.9.0 和 icc 16.0.2,但在 VS 2017 和 Clang 4.0.1 中失败
我已经将问题简化为以下代码片段,当我与库创建者一起解决问题时,为了知识起见,我想知道为什么这段代码在提到的两个编译器中工作,而无法在其他两个编译器中构建。
template <typename A>
struct Expression
{
static const int rank = A::rank_;
};
struct EndIndex : public Expression<EndIndex>
{
static const int rank_ = 0;
};
int main(int argc, char ** argv)
{
return 0;
}
VS 2017 的输出为:
1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Source.cpp
1>d:Testsource.cpp(4): error C2039: 'rank_': is not a member of 'EndIndex'
1>d:Testsource.cpp(7): note: see declaration of 'EndIndex'
1>d:Testsource.cpp(8): note: see reference to class template instantiation 'Expression<EndIndex>' being compiled
1>d:Testsource.cpp(4): error C2065: 'rank_': undeclared identifier
1>d:Testsource.cpp(4): error C2131: expression did not evaluate to a constant
1>d:Testsource.cpp(4): note: failure was caused by non-constant arguments or reference to a non-constant symbol
1>d:Testsource.cpp(4): note: see usage of 'rank_'
1>Done building project "Test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
和 Clang 4.0.1 的输出:
source.cpp:4:37: error: no member named 'rank_' in 'EndIndex'
static const int rank = A::rank_;
~~~^
source.cpp:7:38: note: in instantiation of template class 'Expression<EndIndex>' requested here
struct EndIndex : public Expression<EndIndex>
这可能是因为rank_
在该阶段没有定义。
以下修复了 Apple LLVM 版本 9.0.0 (clang-900.0.38) 的问题:
template <typename A>
struct Expression
{
static const int rank;
};
struct EndIndex : public Expression<EndIndex>
{
static const int rank_ = 0;
};
template <typename A>
const int Expression<A>::rank = A::rank_;
Visual C++ 和 clang 根本无法找到EndIndex
的成员rank_
因为它是在声明之前被访问的。这种花哨的代码通常会导致某些环境中的问题。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 使用函数中的数据初始化C++ const 成员 (MPI_comm_size/rank)