我应该写:
template<class T> class Foo {
typename const T* x;
};
或:
template<class T> class Foo {
const typename T* x;
};
最佳答案
typename
需要与您尝试获取的类型一致。例如,如果你有
template <typename T>
void foo(T& t)
{
typename const T::iterator bar = t.begin();
}
int main()
{
std::vector<int> bar{1,2,3}
foo(bar);
}
typename const T::const_iterator bar = t.begin();
会出现编译错误
expected nested-name-specifier before 'const'
在哪里
const typename T::iterator bar = t.begin();
工作得很好。
有关 template
和 typename
需要出现的时间、地点和原因的全面解释,请参阅:Where and why do I have to put the "template" and "typename" keywords?
关于c++ - 在 C++ 中,typename 应该在 const 之前还是之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898730/