2017-10-18 98 views
4

我正在尝试使用自动差异库Adept,并且使它与gcc 4.9.0和icc 16.0.2一起工作,但是VS 2017和Clang 4.0.1失败错误:'EndIndex'中没有名为'rank_'的成员

我已经将问题简化为以下代码片段,并且在解决库创建者的问题时,为了知识的缘故,我想知道为什么这段代码在两个提到的编译器中工作并失败建立在另外两个。

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 ========== 

而且输出锵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> 
+0

我碰巧发现我一直在寻找另一个CRTP问题的答案。 [链接](https://stackoverflow.com/questions/46576847/clang-vs-gcc-crtp-constexpr-variable-cannot-have-non-literal-type/46578880#46578880) –

回答

3

这可能是因为rank_在该阶段没有定义。

以下修复它为苹果LLVM 9.0.0版(铛-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_; 
+0

此建议也解决了问题VS 2017. –

+0

@ManuelNúñez,感谢您检查! –

0

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

相关问题