Wednesday 15 April 2015

c++ - Prevent compiler from considering implicitly-declared copy constructor in C++03 -


Note that I am working in C ++ 03, and delete +11 my Are not available for.

I am trying to design a non-replicable object, and to prevent the compiler from considering that underlying-declared per constructor on that class. This is a unit test for sustainability I am developing.

Consider that I have two main objects: a core library object, root , and special-case object generated under the test, branch . I am trying to develop a test stability class, stability which handles the drawbacks of setting up & amp; Talking to the core root object, this is a simplified example of what I have made so far:

(With the code given below, I have my own noncopyable is defined, except)

  #include & lt; Boost / utility.hpp & gt; #include & lt; Boost / noncopyable.hpp & gt; Square root {}; Class Stability: Public Promotion: Non-CoCAPPable {Public: Stability (Root and Root): Mairoot (Route) {} Private: Route & amp; MRoot; }; Classroom branch: public route, public stability {public: branch (): stability (* this) {}}; Int main () {branch branch; }   

Compiling these results:

  main.cpp: "branch" in constructor :: branch () a ???? Main.cpp: 30: 23: Error: Overloaded call fixtures (branch & amp;) ??? Main.cpp: 30: 23: Note: Candidates are: main.cpp: 13: 5: Note: Stability :: Stability (root) main.cpp: 8: 7: Note: Stability :: Stability ( Const fixtures & amp;)   

The C ++ 03 compiler is not possible to prevent it from declaring a copy constructor for stability unless I Do not declare at least one on your own. But even: Stability: Public Promotion: Non-Collabelable {Public: Stability (root and root): mRoot (root) {} Personal: Stability (Constant fixtures & Amp;); Stability (stability and;); Route & amp; MRoot; };

... compiler will consider introducing private announcements to stabilization in the branch . Initial list:

  stability (* this)   

I do not want the compiler to consider these copy constructions.

I could

  stability (static_cast & lt; root & amp;; (* this))   

. But I would not want to, because it has the ability to make my nose a little smelly and non-copied words / to promote by constant : noncopyable .

In this case, there is a way to prevent the compiler from considering the built-in per constructor without changing the code on the call-site:

  Sustainability (* )   

?


  • "This is not possible ...": Standard C + 03: 12.8 / 4, "Special Members Functions":
    < P> If class definition is not expired legally declares a copy constructor, one is declared completely.

    Your opacity is that this a Root & amp; and can force both with stability and , and both conversions are equally good (i.e. derivative-to-base conversion).

    The trick is to create a surcharge which is a better match, for example

      template < Typename T & gt; Stability (T & amp; A / C)   

    corresponds to any value exactly , and thus there is a better match than a surcharge that requires conversion .

    However, this is very simple, because in fact you really want to make your stability not worth being manufactured from nothing. Instead, you want it to be the only thing that can disable the external constructor with some SFINAE magic. First C ++ 11 edition:

      #include & lt; Type_traits & gt; Template & lt; Typename T, typename = typename std :: enable_if & lt; Std :: is_base_of & lt; Route, Tac: :: Value & gt; :: type & gt; Stability (t & amp; x): mRoot (x) {}   

    In C +03, we use Boost, and we can not use the default template argument: boost :: is_base_of & gt; root, T & gt; & gt; :: type * = NULL): mRoot (x) {} < / Pre>

    Now you are guaranteed that T has been taken from the route . The overload of this templated manufacturer with T = branch is an exact match, the copy is better than the constructor, and therefore it has been chosen as the best overload.

No comments:

Post a Comment