c++ - gcc 4.6 出现奇怪的模板错误消息

标签 c++ templates gcc4

我从 gcc 4.6 收到一条关于模板成员函数的真正奇怪的错误消息。这是一个最小的例子:

template<typename T>
class Pimpl
{
public:
    Pimpl(const Pimpl&) {}

private:
    T* clone_(const T*);
};

template<typename T>
template<typename P>
T*
Pimpl<T>::clone_(const T*)
{
    return new P(static_cast<const P&>(*p));
}

这是错误:

$ c++ -c y.cpp
y.cpp:14:1: error: prototype for ‘T* Pimpl<T>::clone_(const T*)’ does not match any in  class ‘Pimpl<T>’
y.cpp:8:8: error: candidate is: T* Pimpl<T>::clone_(const T*)

请注意,不匹配的原型(prototype)与候选原型(prototype)完全相同。

什么给出了?

最佳答案

它们之所以不同,是因为 template <typename P> 。 Comeau 错误消息(在 http://www.comeaucomputing.com/tryitout 生成)突出显示了您遇到的问题:

error: template nesting depth does not match the previous declaration of function "Pimpl<T>::clone_"

总的来说值得一提的是:

(a) Comeau 编译器以生成特别好的错误消息而闻名。

(b) 使用多个编译器进行编译通常会给您带来其他方式可能无法获得的见解。

关于c++ - gcc 4.6 出现奇怪的模板错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12773770/

相关文章:

c++ - 如何在 Maya 中创建带边界框的 MPxTransform 节点?

c++ - 使用自身作为参数的模板类。 C++

c++ - 在template<template<class> class T1, class T2>中,<class>是什么意思?

cygwin - 如何在 Cygwin 下使 OpenMP 与 MinGW-64 一起工作?

gcc 7.5 上的 C++ 隐式转换和通用转发

c++ - 默认情况下,GCC 4.3.3 中如何处理 C++ 风格的注释?

Qt 库推荐使用 C++ Qt Creator 版本 4.8.5

c++ - 为什么不能在 STL 映射中插入值?

c++ - 为 vector : template parameters not deducible in partial specialization 定义散列

php - Symfony:扩展原始 AppBundle 模板文件