C 无法解释的类“尚未声明”错误,这是由于名称空间而导致的
c++ Unexplainable class “ has not been declared” error due to namespace
我有一些模板类,有两个私人静态成员。用户定义特征结构并将其提供给模板类,然后从中衍生。
然后在C 文件中,用户定义了静态成员,一个成员从另一个成员开始。 由于某种原因,如果我不完全指定ARG的名称空间,我会得到"尚未声明"错误。这只是一个问题,当我处于嵌套名称空间时,如果您定义单个顶级名称空间中的类型,这没有问题,这使我认为这是一个编译器错误。修剪下面的示例,用GCC 7.2
编译template<typename Traits>
struct Base
{
static int x;
static int y;
};
namespace foo::bar
{
struct BarTraits
{
};
using Bar = Base<BarTraits>;
template<> int Bar::x = 0;
template<> int Bar::y( Bar::x ); //error
//template<> int Bar::y( foo::bar::Bar::x ); //no error
}
根据C 标准temp.expl.spec#3
可以在可以定义相应的主模板的任何范围内声明一个明确的专业化。
您的代码违反了此语句,因为Bar::x
和Bar::y
专门用于namespace foo::bar
。
GCC由于已知的缺陷而错误地接受了前两个专业,https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56119
以下固定代码
template<typename Traits>
struct Base {
static int x;
static int y;
};
struct BarTraits {};
using Bar = Base<BarTraits>;
template<> int Bar::x = 0;
template<> int Bar::y( Bar::x );
被GCC,Clang,MSVC接受:https://gcc.godbolt.org/z/mpxjtzbah
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 我们可以将阈值应用于色彩空间模型的单个组件(如 RGB 和 LAB)吗?
- 为什么 Clang 在命名空间'std'中没有名为 'copy' 的成员给出错误
- 由于相同的文件名/类名存在于不同的SO中而导致的SEG错误
- 初始化命名空间中的变量是否类似于将它们初始化为类成员?
- 独立于编译器的类名
- 名称后跟"::"必须是类或命名空间错误,即使"::"后面是类名
- Catalina C++:使用<cmath>标头产生错误:全局命名空间中没有名为 'signbit' 的成员
- 如何解决两个不使用命名空间的第三方库之间的类名冲突?
- 运算符<<依赖于参数的查找不在全局命名空间中查找
- 命名空间 pcl 在点云库中没有名为 PointCloud 的成员
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- 如何使函数模板仅适用于特定命名空间中的类型
- 在全局命名空间中重载不依赖于用户定义类型的标准定义类型的运算符是否格式正确?
- 类名和枚举值之间的冲突:无需命名空间即可解析
- 我是否可以在类似于 C# 的 C++ 中创建全局命名空间层次结构,以帮助开发人员使用我们的代码?
- 命名空间"std"中没有名为 'vsnprintf' 的成员;你是说"VSPRINTF"吗?
- 当应用于命名空间范围的类时,静态是什么意思?
- C++如何使一个类依赖于名称空间,而该名称空间依赖于该类
- Eclipse CDT/Qt:有一个Qt Gui项目依赖于工作空间中的另一个c++项目