I have a class that should be kept in the context of some data, without the owner of that data (without actual data guarantee Go out of the scope). Especially , the class can not make a copy - the data are easily gigabytes in size.
Now, the general implementation (I think) is for the reference of the data:
Structure Holder_f {Type Const & amp; value; Holder_f (type const & amp; value): value (value) {}}; (Please note that const has absolutely no effect on this problem).
Now, I want to assign (i.e. a functioning operator = ) this class I thought it was a very common problem but remember me Not that how (if ever) I have solved it before. The problem is that one reference can not be specified and there is no way around this bus, instead of the assignment operator, I use the new solution as the only solution:
// x = other_x; Gets replaced with: x. ~ T (); New (and x) t (other_x); Now, it works and the standard is consistent but it is definitely not ugly - unacceptable.
So I'm searching for options to use an idea pointer, but I am unsure whether my constructor is actually guaranteed to work (and it is impossible to pass the pointer due to an interface ):
struct holder_ptr {type CONST * value; // Is it legal? Holder_prasara (type const and amp; value = 0): value (and value) {}}; But I want to use any reference, if possible - How to implement an assignment operator?
struct holder_f {type const & amp; value; Holder_f (type const and value = 0): value (value) {} holder_f & amp; Operator = (Holder_off const & other) {// Now what?! Return * This; }}; As a test case, consider the following code:
int main () {int const TEST1 = 23; Int Construction Construction 2 = 13; Int cost test 3 = 42; Std :: vector & lt; Holder_ptr & gt; Hptr (1); Std :: vector & lt; Holder_ref & gt; Href (2); // Version 1. Indicator HPTR [0] = Holder_PTR (TET1); // version 2. Placement new href [0] ~ holder_ref () .; Newer (& amp; 0] [0]) holder_f (test2); // version 3. ??? Href [1] = holder_f (TEST3); Insert (* HPTR [0]. Value == TEST1); // Works (?) Emphasize (href [0] .value == TEST2); // Works says that (href [1] .value == TEST3); // Boom! } (Also, to clarify this - the way we are talking about is non-pod and I need a standard compliance solution.)
I do not see anything wrong with using holder_ptr . It can be implemented like this:
struct bad_holder: std :: exception {}; Struct holder_ptr {holder_ptr (): value (0) {} holder_peter (type const and amp; value): value (& amp; value) {} type const & amp; () Get () (if (value == 0) throw bad_holder (); the amount of return;} private: type const * value;}; Specify the indicator, you know that you have a valid object (or, you have already ended with "empty reference", in which case you have other problems, because you already have undefined behavior Have been applied).
With this solution, interface references It has been fully implemented in context, but an indicator is used under the hood so that the type is assigned, the use of references in the interface ensures that there is nothing coming along with the use of pointers ( That is, you never worry that the indicator is zero).
Edit : I have updated the example so that the holder can be prepared by default.
Comments
Post a Comment