我有一个 vector :
std::vector<std::weak_ptr<SignalFunction>> m_connections
然后我想声明一个由共享指针组成的 vector ,指向与该 vector 以通用方式由弱指针持有的相同类型:
std::vector<std::shared_ptr<typename typename decltype(m_connections)::value_type::element_type>> validConnections;
它在 Visual Studio 2013 中编译良好,但在 Xcode for iOS 中编译失败。错误:
expected a qualified name after 'typename'
然而,这工作正常:
typedef decltype(m_connections)::value_type ValueType;
std::vector<std::shared_ptr<typename ValueType::element_type>> validConnections
如何避免使用中间体 typedef
?
最佳答案
你只需要一个typename
。当您编写 typename T::some_type::other_type
时,编译器知道 some_type
必须是类型名称,因为紧跟在它后面的是 ::
。只有最后一个元素 (other_type
) 必须显式注释,为此单个 typename
就足够了。
关于c++ - 如何正确写入 "typename typename ..."type scspecifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27617941/