CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Is this legal?

  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Is this legal?

    Hi folks!

    I came around a construct that works, but is a bit strange, to put it mildly. I was wondering if it is legal. I didn't dive into the Standard -- I didn't have the time... Here it is:
    Code:
    template <typename T>
    class A {/*...*/};
    
    class B:public A<B> {/*...*/};
    If you wonder what that's good for: 'A' implements common operations on a container which contains pointer-to-members of B. Sort fo hard to explain. I wasn't able to find a reason why this shouldn't work. OTOH it really doesn't look very healthy...

    Looking forward to reding your comments...
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Is this legal?

    I really think that it is legal.
    B is implicitly partially declared like in a "class B;" statement when the compiler encounters "class B: ....", and a template can perfectly use a partially declared class if it just uses pointers to that class. Of course if the template uses class values (instead of pointers or references), it will generate a compiler error : "class B is not entirely defined at this position and its size is unknown" or something like that.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Is this legal?

    This is the "Curiously Recurring Template Pattern" (see here, for example).

    This pattern is used quite extensively in Microsoft's ATL (or it was when I last used it about three years ago).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Is this legal?

    This is used extensively in ATL. The basic idea is to "aggregate functionality" without using virtual functions. The thing is that in the base class you'll define some functions in the following way:
    Code:
    void A::func(int param)
    {
      // Do something
      // Now we need to call a "compile-time virtual function"
      T *p = static_cast<T *>(this);
      p->somefunction(param);
    }
    There is an article by Zeeshan here which goes into a bit more depth.

    Basically it's a bit faster than runtime polymorphism as used in MFC for example. I've used ATL so much that I don't think it's unhealthy but I can understand why you find it strange.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Is this legal?

    Another example might be to implement intrusive reference counting:
    Code:
     template <class T>
     class refcounted
     {
     protected:
     	friend class SmartPointer<T>;
     
     	void AddRef() { ++count_; }
     	void Release()
     	{
     		if (--refcount == 0)
     			delete this;
     	}
     	refcounted() : count_(0) {}
     	virtual ~refcounted() {}
     
     private:
     	int count_;
     };
     
     class myclass : public refcounted<myclass> {}
    This allows SmartPointer or the derived class to access AddRef and Release, but not any other types. Note also that SmartPointer<U> can't muck about with a T, either.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Is this legal?

    Thanks for your comments, people.

    So I just re-invented the wheel, it seems
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Is this legal?

    And it's fine when T is always a full concrete class.

    When T is part of a hierarchy, you can get a problem as an A<Derived> is not derived from an A<Base>, but your class will derive separately from both and you may get ambiguities.

    You might also decide to create a complete base thus ABase from which all the A<T> derive. Now it's true that A<Derived> and A<Base> both derive from ABase but you have the inheritance diamond, so you'll need to derive A<T> virtual from ABase.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured