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

前端 未结 3 2025
名媛妹妹
名媛妹妹 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条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 11:56

    Extending @jpalecek's idea, we could make that template argument take a default argument. But you need to enable C++0x to get this

    #include 
    #include 
    
    template
    class base
    {
    public:
        template    // <-- (requires C++0x to have a default)
        void foo(typename X::dummy& d)
        {
            printf("%s\n", typeid(d).name());
        }
    };
    
    template
    class derived
      : public base< derived >
    {
    public:
      typedef DUMMY dummy;
    };
    
    struct tag{};
    
    int main()
    {
      derived foo;
      tag t;
      foo.foo(t);       // <--- call the function like normal.
    }
    

    http://ideone.com/AXXdW

提交回复
热议问题