本文将分享在python中复制构造函数?的详细内容,并且还将对python拷贝构造函数进行详尽解释,此外,我们还将为大家带来关于c–boost::python:编译失败,因为复制构造函数是私有的、c–
本文将分享在python中复制构造函数?的详细内容,并且还将对python拷贝构造函数进行详尽解释,此外,我们还将为大家带来关于c – boost :: python:编译失败,因为复制构造函数是私有的、c – 为什么不调用复制构造函数?、c – 为什么调用复制构造函数而不是移动构造函数?、c – 类型t = Type()是否调用复制构造函数?的相关知识,希望对你有所帮助。
本文目录一览:- 在python中复制构造函数?(python拷贝构造函数)
- c – boost :: python:编译失败,因为复制构造函数是私有的
- c – 为什么不调用复制构造函数?
- c – 为什么调用复制构造函数而不是移动构造函数?
- c – 类型t = Type()是否调用复制构造函数?
在python中复制构造函数?(python拷贝构造函数)
python中是否有复制构造函数?如果没有,我将怎么做才能达到类似的目的?
情况是,我正在使用一个库,并且在其中扩展了其中一个类,并提供了额外的功能,我希望能够将从库中获得的对象转换为自己的类的实例。
c – boost :: python:编译失败,因为复制构造函数是私有的
C类看起来像这样(简化的)
class Foo { public: Foo(const char *name); // constructor private: ByteArray m_bytearray; };
ByteArray类继承自boost :: noncopyable,因此Foo没有复制构造函数.
这是python模块存根:
BOOST_PYTHON_MODULE(Foo) { class_<Foo>("Foo",init<const char *>()) ; }
当编译boost :: python模块时,我收到错误,因为ByteArray继承自boost :: noncopyable,所以无法创建Foo的副本构造函数.
我如何禁用我的python模块中的复制构造函数?
谢谢
克里斯托夫
解决方法
BOOST_PYTHON_MODULE(Foo) { class_<Foo,boost::noncopyable>("Foo",init<const char *>()) ; }
c – 为什么不调用复制构造函数?
请考虑以下代码.
struct A { typedef std::vector<double> State; // template <class... Args> // A(Args... args) // : a(args...) // {} template <class... Args> A(Args&&... args) : a(std::forward<Args>(args)...) {} A(const A&) = default; A(A&&) = default; State a; }; int main(){ A a(3,2); A b = a; // This line triggers an error!! }
Gcc 4.8.0无法使用错误消息进行编译
错误:调用’std :: vector< double> :: vector(A&)’的匹配函数:a(std :: forward< Args>(args)…).
我不明白为什么这个代码是错误的.在我看来,编译器应该调用A行= a的行中的复制构造函数.
但是,如果我用已注释的替换构造函数(简单地取值).它编译.此外,现在不需要默认的复制(和移动)构造函数.
这里发生了什么?
解决方法
所以在你的情况下,你正在通过A,因此Args … = A& Args&& … = A& &&,A&&由于引用崩溃规则,它比const A& amp;更好的匹配,因为编译器不必为非const变量添加const.
c – 为什么调用复制构造函数而不是移动构造函数?
class Outer { class Inner { public: Inner(Inner&& i):outers(std::move(i.outers)),test(std::move(test)) {} void addOuter(const Outer& o) {outers.push_back(std::move(o));} private: std::vector<Outer> outers; std::unique_ptr<std::string> test; }; public: Outer(Outer&& o):inners(std::move(o.inners)) {} private: std::vector<Inner> inners; };
当我尝试在Visual Studio 2012上编译上面的代码时,我收到以下错误:
错误1错误C2248:’std :: unique_ptr< _Ty> :: unique_ptr’:无法访问类’std :: unique_ptr< _Ty>‘中声明的私有成员
显然,编译器调用复制构造函数而不是addOuter方法中的push_back中的移动构造函数.这是编译器错误吗?如果不是为什么,对于这个特定情况,是不是调用了移动构造函数?
解决方法
c – 类型t = Type()是否调用复制构造函数?
我问,因为当我尝试:
#include <iostream> class Test { public: Test(Test const &) { std::cout << "hello"; } test() { } }; int main() { Test t = test(); return 0; }
没有输出,但当我改变它
#include <iostream> class Test { Test(Test const &) { std::cout << "hello"; } public: test() { } }; int main() { Test t = test(); return 0; }
我明白了:
error C2248: 'Test::Test' : cannot access private member declared in class 'Test'
这没有意义(特别是因为这是一个调试版本).
更新:
即便这样编译!
struct Test { Test(Test &&) = delete; Test(Test const &) = delete; test() { } }; int main() { Test t = test(); return 0; }
复制/移动构造函数是必需还是否?
解决方法
In C++ computer programming,copy elision refers to a compiler
optimization technique that eliminates unnecessary copying of objects.
The C++ language standard generally allows implementations to perform
any optimization,provided the resulting program’s observable behavior
is the same as if,i.e. pretending,the program was executed exactly
as mandated by the standard.The standard also describes a few situations where copying can be
eliminated even if this would alter the program’s behavior,the most
common being the return value optimization. Another widely implemented
optimization,described in the C++ standard,is when a temporary
object of class type is copied to an object of the same type.[1] As
a result,copy-initialization is usually equivalent to
direct-initialization in terms of performance,but not in semantics;
copy-initialization still requires an accessible copy
constructor.[2] The optimization can not be applied to a temporary
object that has been bound to a reference.
您正在进行复制构造,但是标准允许将其转换为直接初始化,并且无论调试是否关闭都可以完成,这就是打印未到达的原因.
但是,因为它“应该”是一个复制结构,你需要访问一个,这就是第二个代码不起作用的原因.
今天关于在python中复制构造函数?和python拷贝构造函数的介绍到此结束,谢谢您的阅读,有关c – boost :: python:编译失败,因为复制构造函数是私有的、c – 为什么不调用复制构造函数?、c – 为什么调用复制构造函数而不是移动构造函数?、c – 类型t = Type()是否调用复制构造函数?等更多相关知识的信息可以在本站进行查询。
本文标签: