"New thinking of C + + design" Corrigendum, c++14 New Solution

Source: Internet
Author: User

Errata

The original book (Chinese version) 3.13, 65-69 pages Genscatterhierarchy and fieldhelper are problematic, when the type in the typelist is duplicated, cannot be compiled (because of "ambiguity base class").

The two semantic questions that appear in the book can be illustrated with a small snippet of code:

class a{}; class B: public a{}; class C: public A, public b{}; void Test () {    c C;    A& CF =c; // Wrong,don ' t try this at home.      b& CBF =c; //  Right    a& caf = CBF; // Right}

Since c inherits two A, a direct inheritance, an indirect inheritance, C is converted to a, there are two paths, cannot be converted. Even we can never access C directly inherit the A!

The path to inherit B is unique, so it can be converted to a again by B.

The Code and class diagrams given in the "new thinking of C + + Design" book are unique to the right, but the path to the left is not one, so typelist cannot be compiled once it contains a repeating type.

This problem has been solved, the specific solution please refer to Loki source code Loki source

Let me briefly say here that the solution is to make sure that the immediate parent class on the left side is unique so that a unique path can be converted to a base class.

C++14 new law:

First, the two functions of typelist and Operation Typelist are given.

Template <typename ... T>structtypelist;template<intI, TypeName Arg>structat;template<intI, TypeName Head, TypeName ... Tail>structAt<i, Typelist{typedef typename at<i-1, typelist<tail...>>:: type type;}; Template<classHead, TypeName ... Tail>structat<0, Typelist{typedef Head type;}; Template<intNclassSeq>structdrop;template<intNclassHead,class... Tail>structDrop<n, Typelist{typedef typename Drop<n-1, typelist<tail...>>:: type type;}; Template<classHead,class... Tail>structdrop<0, Typelist{typedef typelisttype;};

At gets the type element of an index value in Typelist, and the drop is a number of elements that remove the header.

The Genscatterhierarchy code is given below to facilitate the use of the Genorator generation.

namespaceprivate{//The following type helps to overcome subtle flaw in the original//implementation of Genscatterhierarchy.//The flaw is revealed when the input type list of Genscatterhierarchy//Contains more then one element of the same type (e.g. loki_typelist_2 (int, int)).//In this case Genscatterhierarchy would contain multiple bases of the same//type and some of them won't be reachable (per 10.3).//For example before the fix the first element of tuple<loki_typelist_2 (int, int) >//Is isn't reachable in any way!template<class,class>structUniqueTag;} Template<typename head,typename ... Tails, Template <typename>classUnit>classGenorater<typelist        : PublicGenorater<private::uniquetag PublicGenorater<typelist<tails...>,unit>{};template<typename head,typename ... Tails,template <typename>classUnit>classGenorater<private::uniquetag Publicunit

UniqueTag, which is used to assist in building a unique type conversion path.

The following gives the Fieldhelper source code:

Template <intI,typename ... Tlist,template <typename>classUnit> unit<typename at<i,typelist<tlist...>>::type>& FieldHelper (Genorater<TypeList <TList...>,Unit>&obj) {Genorater<private::uniquetag<typename At<i,typelist<tlist...>>::type,typename drop<I,TypeList< tlist...>>::type>,unit>& leftbase =obj; returnLeftbase;} Template<intI,typename ... TList, Template <typename>classUnit> unit<typename at<i,typelist<tlist...>>::type>& Field (Genorater<TypeList< tlist...>,unit>&obj) {     //return Fieldhelper (obj,int2type<i> ());    returnFieldhelper<i>(obj);} 

Finally, the test code:

typedef typelist<Double,int,Double,string>mylist;typedef Genorater<myList,Holder>Info;intMain () {cout<<"Hello world!"<<Endl;    Info obj; cout<<typeid (obj). Name () <<Endl; Field<1> (obj). value_=1; Field<2> (obj). value_=2; Field<3> (obj). value_="hao123"; cout<< field<1> (obj) .value_<<Endl; cout<< field<2> (obj) .value_<<Endl; cout<< field<3> (obj) .value_<<Endl; return 0;}

-------------------------------------------------------------------------Gorgeous split-line------------------------------------------ ---------------------------------------------

Here is a copy of the Loki source code Fieldhelper

namespaceprivate{template<class,class...>structUniqueTag;} Template<typename head,typename ... Tails, Template <typename>classUnit>classGenorater<typelist        : PublicGenorater<private::uniquetag PublicGenorater<typelist<tails...>,unit>{};template<typename head,typename ... Tails,template <typename>classUnit>classGenorater<private::uniquetag PublicUnit{};template<typename head,typename ... Tlist,template <typename>classUnit> unit0>) {Genorater<Private::UniqueTag<Head,TList...>,Unit>& leftbase =obj; returnLeftbase;} Template<intI,typename head,typename ... TList, Template <typename>classUnit> unit<typename at<i,typelist) {Genorater<TypeList<TList...>,Unit>& rightbase =obj;returnFieldhelper (rightbase,int2type<i-1>());}

Thank you, reprint please indicate the source! This article only read "C + + Design new thinking" a friend useful, other Bo friends carefully read (do not have a resistance to C + +)

"New thinking of C + + design" Corrigendum, c++14 New Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.