编译器未为类的构造函数抛出错误有语法错误
Compiler not throwing error for constructor of class has syntax error
我有以下一段代码,我真的犯了一个语法错误:
#include<iostream>
using namespace std;
template<typename Type1,typename Type2>
class Pair{
public:
Type1 first;
Type2 second;
Pair();
Pair(Pair<Type1,Type2>& obj);
};
template<class Type1,class Type2>
Pair<Type1,Type2>::Pair(){
first=Type1();
second=Type2();
}
template<class Type1,class Type2>
Pair<Type1,Type2>::Pair(Pair<Type1,Type2>& obj1){
cout<<"Inside the copy constructorn";
obj1.first= //THIS IS THE PROBLEMATIC STMNT
}
int main()
{
/* Code here */
Pair<int,int> com1;
//Pair<complex1,complex2> com2(com1);
}
我在这个程序中没有发现任何编译/运行时错误。但是,如果我取消main中调用复制构造函数的第二行注释,则会抛出编译器时间错误。我知道类在运行时是根据类型进行初始化的,但是像这样的语法错误肯定会在编译阶段在模板化的类中进行检查。为什么没有编译时错误?
Pair<complex1,complex2> com1;
使用默认构造函数,而Pair<complex1,complex2> com2(com1);
使用复制构造函数。
由于没有第二行,永远不会使用复制构造函数,因此不会编译;编译器从不为它生成代码,因此它从不检查它是否可以编译。
这是一个编译器错误。您可以用一种更简单的方式复制它:
template <class T>
void f()
{
= // should be an error, but is not in MSVC
}
int main()
{
}
这将在非MSVC编译器中创建一个类似error: expected primary-expression before '=' token
的诊断消息,但在MSVC中可以正常编译。
明显的原因是MSVC仍然没有实现c++标准所要求的两阶段查找。正如MSVC开发人员Stephan T. Lavavej最近在Visual c++团队博客中解释的那样:
VC还没有实现c++ 98/03的三个特性:两阶段名称查找,动态异常规范,并导出。两阶段名称查找在2015年仍未实现,但它在编译器团队的列表中要做的事情,等待代码库现代化
相关文章:
- 分段错误当我试图运行程序时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 制作时出错,C++"db.c:2127:错误:在"||"标记之前应使用";"
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 安装 mply 库时出错.致命错误:gsl/gsl_sf.h:没有这样的文件或目录
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 可能的 GCC 链接器错误会导致将弱符号和局部符号链接在一起时出错
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 为 freebsd 11 编译 gcc4.8.5 时出错: 错误: 未知类型名称 'choke'
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 我同时使用了莱布尼茨和瓦利斯公式来估算π但输出数字错误,我不知道我哪里出错了
- 显示错误消息框时出错.我该如何显示它
- 使用 swig 时出错:输入 (1) 中的语法错误
- 编译类 C++ 中的线程时出错(错误 xthread)
- 在 Visual Studio 2015 社区中消除错误时出错