c++ - GCC Shared_ptr 模板错误

标签 c++ templates g++ shared-ptr

下面的函数

#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/

相关文章:

C++ 模板特化问题

c++ - g++ 在构建点云库时未定义对符号 '__cxa_free_exception@@CXXABI_1.3' 的引用

c++ - 确定注册表项是否包含注册表值或子项的最佳方法是什么?

c++ - 模板和访问者模式

css - 使两个CSS类相同

c++ - Nana 用 C++ 制作 GUI

gcc - CentOS 7 升级了 gcc/g++ 但似乎无法使用?

c++ - 确定正确的原点以围绕 QPolygonF 旋转

c++ - 为什么这个 C++ 代码在不同的编译器上给出不同的输出?

c++ - 编译器如何实现 constexpr 函数?