下面的函数
#include <memory>
template<typename T>
std::shared_ptr<typename T> Tail(const std::shared_ptr<typename T>& cont, size_t n)
{
const auto size(std::min<size_t>(n, cont->size()));
return std::shared_ptr<typename T>(new T(cont->end() - size, cont->end()));
}
在 gcc 4.7.2 上产生以下错误。
g++ test.cpp -std=c++0x
test.cpp:4:27: error: template argument 1 is invalid
test.cpp:4:66: error: template argument 1 is invalid
test.cpp: In function ‘int Tail(const int&, size_t)’:
test.cpp:6:42: error: base operand of ‘->’ is not a pointer
test.cpp:7:35: error: template argument 1 is invalid
test.cpp:7:47: error: base operand of ‘->’ is not a pointer
test.cpp:7:67: error: base operand of ‘->’ is not a pointer
我知道 cont“看起来”不像指针,但这在 VS2012 上编译得很好。 如何为 gcc 编写函数?
最佳答案
只需删除那些额外的typename
template<typename T>
std::shared_ptr<T> Tail(const std::shared_ptr< T>& cont, size_t n)
{
const auto size(std::min<size_t>(n, cont->size()));
return std::shared_ptr< T>(new T(cont->end() - size, cont->end()));
}
关于c++ - GCC Shared_ptr 模板错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15631612/