Topic: Replace templates with type builder objects


Author: nemethpeter<hun.nemethpeter@googlemail.com>
Date: Tue, 18 Sep 2012 08:24:21 -0700 (PDT)
Raw View
Hi!

I started to think on the subject. So I want to replace the template instantiation in C++ compiling with "normal functions (means compile time programs that works on compiler parse tree or something similar)", and I don't want to use the declerative syntax.

Do you think that with the following idea we can replace the whole template mechanism in C++? Do you think that this way result an easier to understand templating? Do you think that this is a right direction?

I asked this question on stackoverflow (http://stackoverflow.com/questions/12448723/is-c-template-mechanism-is-just-a-type-builder-funtion) and somebody said that this is the right place for it.

This is just an idea, and I just started to thinking on for 1-2 days, so don't expect too much details now...

So here is my proposal for new syntax:
// old syntax
template<typename T>
struct A
{
   A<T>();                   // constructor
   ~A<T>();                  // destructor
   int foo(int, bool) const; // method
   bool bar;                 // member
   T data;                   // templated member
};

// new syntax
#if 0

static()
{

// classes from here: http://clang.llvm.org/doxygen/classclang_1_1Stmt.html
class Stmt
{
};

class IfStmt : public Stmt
{
};

}

// define class as metaclass
metaclass class
{
   string name;                              // static string
   declaration body;                         // declaration ??
   vector<const class*>  inherits;            // static vector
};

class NormalClass1
{
};

// a function that returns a class
class BuildA(typename T)
{
   class ret;                               // class object can only declared in class builder
   ret.name = "A";
   ret.inherits += (public NormalClass1, public NormalClass2);
   ret.body +=
          {
            ();                             // [0] constructor
            ~();                            // [1] destructor
            int (int, bool) const;          // [2] unnamed method
            bool bar;                       // [3] member
            T data;                         // [4] templated member
          }
   param test_param1 = (int p_test1);
   param test_param2 = (long p_test);
   test_param1.type = char;
   test_param1.name = "p_test1_renamed";
   vector<param>  params;
   params.push_back(test_param1);
   params.push_back(test_param2);
   ret.body[constructor].params += params; // add parameters to constructor

   ret.body[2].name = "foo";               // name the unnamed method

   return ret;
}

}
#endif

int main()
{
   A<int>  a;
#if 0
   // create a new class
   typedef new BuildA(int) AInt;
   // or
   typedef new BuildA(int); // in this case class.name must be an initialized thing
#endif
}


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]