C 编译器可以在用户定义和编译器生成的复制构建器之间进行自由选择
c++ compiler free to choose between user defined and compiler generated copy-constructors?
我有以下示例代码(我的程序的删除版本)
类'Some_class'具有带有默认参数的构造函数。编译器能够识别该构造函数是复制构造函数。在主函数中,当我订购一个称为" b"的副本构造对象时,该构造函数被称为。但是,当我从函数结果构造" C"时,编译器调用编译器生成的复制构造函数(复制位模式)。我可以通过c.some_data的值来分辨,这应该由我自己的复制构造函数设置为2。
1)标准对此有何评价?2)我的编译器打破了吗?
我使用mingw没有任何选项,只有我的源文件名的规范和可执行文件的名称。我从官方Mingw网站上获得了GNU开源编译器的港口,我正在使用最新版本。我找到了一个错误,还是由于我对C 的理解?
预先感谢
#include <iostream>
#include <string>
class some_class
{
public:
some_class(int p = 0) :
some_data(p)
{
std::cout << "user defined constructor (p = " << p << ")" << std::endl;
}
some_class(const some_class &, int = 0)
{
std::cout << "user defined copy constructor" << std::endl;
some_data = 2;
}
int some_data;
};
extern some_class some_function_returning_some_class_object();
int main(int, char **)
{
std::cout << "creating a, with no parameters" << std::endl;
some_class a;
std::cout << "creating b, a copy of a" << std::endl;
some_class b = a;
std::cout << "creating c, copy constructed from a function result" << std::endl;
some_class c = some_function_returning_some_class_object();
std::cout << "c.some_data = " << c.some_data << std::endl;
}
some_class some_function_returning_some_class_object()
{
some_class a(1);
return a;
}
输出如下:
creating a, with no parameters
user defined constructor (p = 0)
creating b, a copy of a
user defined copy constructor
creating c, copy constructed from a function result
user defined constructor (p = 1)
c.some_data = 1
编译器未使用编译器定义的默认复制构造函数。大概是使用返回值优化来完全跳过副本。
相关文章:
- VS 2017 使用交叉编译器构建 x64 项目
- 使用Bazel构建具有不同编译器/链接器选项的C/C++依赖关系
- 在 Azure DevOps 构建管道中使用英特尔C++编译器为 Linux 环境构建C++代码
- 在为 iOS 构建 aws-sdk-cpp 时,cmake 上的 CXX 编译器错误已损坏
- C++使用较新的编译器构建应用程序,而无需重新构建库
- 无法构建 quazip。MOC 编译器不会运行。命令行未找到相关类
- 如何使用Visual Studio编译器使用cgo构建库?
- 在Travis中为CMAKE构建指定编译器版本
- 是否有编译器标志可以使较新的 gcc 版本像旧版本一样构建
- 尝试将Winelib与标准macOS构建工具一起使用:得到奇怪的编译器错误
- 构建 C/C++ 代码时的 Gradle 编译器输出
- 尝试使用VC++编译器构建Qt应用程序:"type_traits":没有这样的文件或目录
- 如何安全地部署使用升级的编译器构建的应用程序
- 如何使用 Solaris Studio 12.4 编译器构建 Boost V1.57
- 如何使用 cmake 使用不同的编译器构建多个可执行文件
- 如何用open64编译器构建boost库
- 编译器是用以前版本的编译器构建的吗?
- Sublime Text:编译器构建选项
- Netbeans首次编译器构建错误
- 用Intel编译器构建Boost库