error: no member named 'rank_' in 'EndIndex'

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:\Test\source.cpp(4): error C2039: 'rank_': is not a member of 'EndIndex'
1>d:\Test\source.cpp(7): note: see declaration of 'EndIndex'
1>d:\Test\source.cpp(8): note: see reference to class template instantiation 'Expression<EndIndex>' being compiled
1>d:\Test\source.cpp(4): error C2065: 'rank_': undeclared identifier
1>d:\Test\source.cpp(4): error C2131: expression did not evaluate to a constant
1>d:\Test\source.cpp(4): note: failure was caused by non-constant arguments or reference to a non-constant symbol
1>d:\Test\source.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>

Visual C++ 和 clang 根本无法找到 EndIndexrank_ 成员,因为它在声明之前被访问。这种花哨的代码在某些环境中经常会导致问题。

这可能是因为 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_;