c++ - How do I strongly typedef non-primitive types? -
Considering the following program in which one function accepts both type of expected type and type of typingfif type is.
// A user defined type class widget {}; // A function that creates a widget zero function (widget w) {} int main () {// Type (it is C ++ 11 syntax for typingfifth. This is the same thing) Gadget = Using Widget ; // Make two "different types" (well .. they are not really different as you will see) widget w; Gadgets; // Call a function which should only accept the widget function (w); // work (good) work (g); // & amp; Lt; - Works (I do not want to compile this)} As you can see, a type-tof actually does not separate a new type. I thought to inherit the type instead: // Instead of the class gadget heir: public widget {}; // two "different types" widget w; Gadgets; // should call the function which should only accept the widget function (w); // work (good) work (g); // & lt; - Works (I do not want this to be compiled) There is only one problem. Paying attention to promotion, I tried a strong typedef:
#include & lt; Boost / serialization / strong_typedef.hpp & gt; // A user defined type class widget {}; // a function that defines the user defined type zero function (w) {} int main () {// strongly typedef BOOST_STRONG_TYPEDEF (widget, gadget) // two "different type" widget w; Gadgets; // should call the function which should only accept the widget function (w); Ceremony (g); } Compiled Errors: in the member function: ??? bool main () :: Gadget :: Operator == (const (main) :: Gadget and;) Context: Error: There is no match for any operator == A ???? (Operand types are - Cant Widget and Content Widget). BOOST_STRONG_TYPEDEF (widget, gadget) ^ Member Function in-bool main () :: Gadget :: Operator & lt; (const Main () :: Gadget & amp;) Context: Error: There is no match for any operator & lt;? (Operand types are- Cunts Widget and 'Cunts Widget).) BOOST_STRONG_TYPEDEF (Widget, Gadget) ^ Apparently BOOST_STRONG_TYPEDEF only works on primitive types.
I tried to inherit again, but to stop the underlying conversion:
// I want functionality, but these are not the same type! Class Gadget: Public Widget {Operator Widget () = Delete; }; This does not work either.
Question: - Why strong_prothic only promotes primitive type?
- Can I type a non-primitive type to gain robust functionality to promote strong typography? Actually you need two unrelated classes with the same behavior. I would use a parametrized template for it:
template & lt; int tag & gt; Class widgetgate {...}; Type widget widget & lt; 0 & gt; Widget; Type widget widget & lt; 1 & gt; Gadget;
Comments
Post a Comment