C++ CRTP and accessing derived's nested typedefs from base

前端 未结 3 2064
名媛妹妹
名媛妹妹 2021-02-05 10:58

edit: I\'ll put a github link here when I am done altering my design for anyone who is interested.

Background

I\'m replacing a

3条回答
  •  Happy的楠姐
    2021-02-05 11:41

    Another possibility (that might or might not save you keystrokes) would be not using the derived classes' nested types in the parent in some places. Eg. instead of

    void foo(typename TWO::dummy & d);
    

    you'd use

    template 
    void foo(typename T& d);
    

    For extra points, you could use SFINAE to actually limit T to the types permissible for the original variant. (Note that inside nested templates, TWO::dummy can be used freely - they are only instantiated after the whole thing incl. derived has been, so it works out. In the naive version, derived is still incomplete at the point of instantiating the base with its member functions, it has no ::dummy, which is why it fails)

提交回复
热议问题