object-lifetime

How to fix: value may contain references; add `'static` bound to `T`

谁说胖子不能爱 提交于 2019-12-24 18:09:11
问题 I managed yet again to run into a lifetime issue that I seem to be unable to solve on my own. I have this trait pub trait MiddlewareHandler: Clone { fn invoke (&self, req: &Request, res: &mut Response) -> bool { true } // we need this because otherwise clone() would be ambiguous fn clone_box(&self) -> Box<MiddlewareHandler> { box self.clone() as Box<MiddlewareHandler> } } impl MiddlewareHandler for fn (req: &Request, res: &mut Response) -> bool { fn invoke(&self, req: &Request, res: &mut

Optimize InputIterator dereference without making a copy if possible?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 17:30:37
问题 I have a legacy code in which the interface is defined for pointer only and I am trying to adapt some functions to take iterators. In the answers to this question Address of a dereferenced InputIterator? The case of istream_iterator it was noted that std::istream_iterators are InputIterator . However they are special among InputIterator s, because their dereference is guarantied to generate a language reference T const& . The code for a general input iterator would look like this, notice that

Reference parameter lifetime

倖福魔咒の 提交于 2019-12-24 03:01:38
问题 Given the following: class ParamClass {...}; class MyObject { public: void myMethod(ParamClass const& param) { _myPrivate = param; } private: ParamClass _myPrivate; } [...] MyObject obj; void some_function(void) { ParamClass p(...); obj.myMethod(p); } What will happen to _myPrivate at the end of the lifetime of object p? EDIT: will I still be able to use _myPrivate to access a copy of object p? Thanks! Dan 回答1: Since _myPrivate is not a reference, in the assignment _myPrivate = param , its

When a union object is copied, is a member subobject created?

瘦欲@ 提交于 2019-12-22 08:40:08
问题 When another member of a union is accessed, the C++ standard used to be silent on what happens, but that was fixed to explain that member access to a union object was allowed for the purpose of assigning to that yet no existent object, which would magically create the object, by assignment to it or one of its members. Essentially the member access operator returns a promise of a future object and you have to use it with an assignment. Given union U { int i; long l; }; We can create a i or l

Dependency injection and life time of IDisposable objects

眉间皱痕 提交于 2019-12-22 08:17:22
问题 I am trying to develop a library using dependency injection approach (with Ninject) and I am having some kind of confusion likely because of my incorrect design. In summary, my design approach is A parent object has a common object. A parent object uses some variable number of child objects. All child objects should use the very same common object instance with their parent object Here is a simple model of my problem domain. interface IParent : IDisposable { void Operation(); } interface

Is this a proper use of a temporary std::string?

风格不统一 提交于 2019-12-22 07:59:12
问题 std::string getMyString() { return <make a string>; } ... HANDLE something = OpenSomething(getMyString().c_str(), ...); I've read Guaranteed lifetime of temporary in C++ and I believe that the temporary string will live until the assignment has been evaluated, i.e. plenty long enough to make this work as expected. Having once before run into an std::string lifetime-related bug (can't remember what it was) I'd rather double-check... 回答1: Yes, this is fine. :-) The string will be destroyed at

Can I override Dispose to make an entity class that always calls SaveChanges?

会有一股神秘感。 提交于 2019-12-21 20:19:54
问题 This is a fairly fine point, and I expect the answer is "it's not a hot idea to begin with" - that said, it has a points that I'm interested in regardless, if someone is kind enough to indulge. Model Code: public partial class MyEntities : ObjectContext { // the idea is if the object is in a using block, this always gets called? protected override void Dispose(bool disposing) { this.SaveChanges(); base.Dispose(disposing); } } Client Code: using(var model = new MyEntities()) { // do something

Does guaranteed copy elision work with function parameters?

六眼飞鱼酱① 提交于 2019-12-21 13:12:13
问题 If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the following code? Foo myfunc(Foo foo) { return foo; } auto foo = myfunc(Foo()); // will there be copies? 回答1: In C++17, prvalues ("anonymous temporaries") are no longer objects. Instead, they are instructions on how to construct an object. They can

Does guaranteed copy elision work with function parameters?

隐身守侯 提交于 2019-12-21 13:10:04
问题 If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the following code? Foo myfunc(Foo foo) { return foo; } auto foo = myfunc(Foo()); // will there be copies? 回答1: In C++17, prvalues ("anonymous temporaries") are no longer objects. Instead, they are instructions on how to construct an object. They can

Should this C++ temporary binding to reference member be illegal?

只谈情不闲聊 提交于 2019-12-21 13:09:32
问题 My question (which will follow after this, sorry about the long intro, the question is down there in bold ) is originally inspired by Item 23 in Herb Sutters Exceptional C++ where we find something like this: <snip> ... int main() { GenericTableAlgorithm a( "Customer", MyWorker() ); a.Process(); } with class GenericTableAlgorithm { public: GenericTableAlgorithm( const string& table, GTAClient& worker ); bool Process(); private: struct GenericTableAlgorithmImpl* pimpl_; //implementation };