Thread: Barton-Nackman trick (?)

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    Barton-Nackman trick (?)

    Hi guys & girls,

    I do have the following question. Let's say I have a skeleton for some functionality, which I want to implement in different ways. One way is the Template pattern, but that uses a polymorphic call to my (protected) implementation function.
    Instead, I want to use a static interface using the Barton-Nackman trick. But then, if I want to store a list of these algorithm implementations in some algorithm-container class, i'm in trouble. Do I not escape from a heterogenous container with "boost::any" ?

    Here's my code :

    Code:
    template<typename Type>
    class Document
    {
    public:
      virtual ~Document(){}
      void open() { read(); }
      void save() { }
      void close() { }
    protected:
      Type& read() { return static_cast<Type&>(this)->read(); }
    };
    
    struct latex : public Document<latex>
    {
      void read(){ }
    };
    
    class Application
    {
    public:
      template<typename Type>
      void addDocument() { document_ = new T; } // suppose T has default constructor and is POD
      ~Application() { delete document_; } // don't bother the raw pointer..
      void OpenDocument() { document_->open(); }
    protected:
      Document* document_;
    };
    
    
    int main()
    {
      Application emacs;
      emacs.addDocument<latex>();
      emacs.OpenDocument();
      return 0;
    }
    Thanks for suggestions !

    Mark

    ps : is this the BN trick? or is this already a crtp.. i have the impression that a crtp is merely the use of the BN trick in template expressions

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    One way is the Template pattern, but that uses a polymorphic call to my (protected) implementation function.
    This is true. You have explicitly stated you want polymorphism. The template method (the actual class method you invoke in the example code) is polymorphic. Where is the problem? Do you mean virtual method call?

    I want to use a static interface using the Barton-Nackman trick.
    No. You don't. Building interfaces using templates instead of inheritance for polymorphism is a fine thing, if you understand the consequences, but that has nothing to do with the "BN Trick".

    The "BN Trick" is a tool used to provide a certain interface based on functionality defined elsewhere at a target scope without having to code the specific interface for every implementation. It is not the interface itself.

    If you dropped the ridiculous attempt at the "BN Trick" and simply wrote your algorithms to a set of interfaces the generic mechanism of templates would use the interfaces related to the types involved. That is what templates do; that is their purpose.

    Do I not escape from a heterogenous container with "boost::any" ?
    Considering the way `boost::any' works, you aren't going to gain anything by what you envision. You'll still have to build your entire system around generics to get the polymorphism you seek. Also, in using `boost::any' you would be using a virtual method call.

    is this the BN trick? or is this already a crtp.. i have the impression that a crtp is merely the use of the BN trick in template expressions
    O_o

    What?

    I don't know what you've been reading, but the "BN Trick" relies on "CRTP" for implementation.

    You can not have the "BN Trick" without "CRTP".

    You can use "CRTP" for a lot of things, but "CRTP" is not the "BN Trick".

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The reversed-comparison trick: an argument against
    By brewbuck in forum Tech Board
    Replies: 8
    Last Post: 02-24-2009, 09:25 PM
  2. No! Another way to trick us men!!
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-18-2004, 09:47 PM
  3. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM
  4. capitalization trick
    By volk in forum C++ Programming
    Replies: 11
    Last Post: 04-05-2003, 07:12 PM
  5. a trick utterly !
    By black in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2002, 08:56 PM