Question

I just started to think on the subject. Every C++ template can be replaced with a "normal" function that returns a class (or function) object? Normal means compile-time program.

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? The question a little bit theoretical I know, but I don't know where is the best place to discuss this kink of question.

template<typename T>
struct A
{
  int foo();
  bool bar;
  T data;
};

#if 0
class A(typename T) // class bulder after "("
{
  class ret;          // class object can only declared in class builder
  ret.name = "whatever_compile_time_string";
  ret += body         // body is a class builder member with class declaration syntax
         {
           body();    // constructor
           ~body();   // destructor
           int foo(); // method
           bool bar;  // member
         };
  ret += member(T, "data"); // "inject" a templated member

  return ret;
}
#endif


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

Solution

Yes. What you are describing is a macro system. The C preprocessor provides a weak form of macros, but, in the general case, macros are nothing more or less than programs to write source code (taking source code as input), executed at compile time.

Now, the theory of macros turns out to be somewhat complicated. Obviously, you can do whatever you want with any macro system that lets you execute code at compile time, but a lot of work has gone into making it so that doing the right thing is easy. (In particular, it really helps to have syntax quotation construct that you can interpolate into.)

With a powerful macro system, many statements that were previously built into the language can instead be user-defined, or included in the standard library.

If you're interested in the subject, take a look at Scheme, and especially the Racket programming language. Racket has a very small core, and almost all of the language that the user experiences is built out of macros.

OTHER TIPS

No. One of the main things about templates is that they provide statically inferred typesafe polymorphism.

By definition, you can't do that at runtime. If you want to move everything to runtime, don't use C++, use something optimised for late binding.

Update: If you're talking a function that is run at compile-time, then (a) you will need your compiler to interpret C++ (if you want to write it in C++), and you lose the benefits of the declarative, functional language that is templates. I don't see the benefit.

There are two ways used by C programmers to achieve similar effects like templates in C++. However I do not see any beneficial from using these ways instead of templates in C++. But if you think templates are hard to understand, then look at these:

By using macros:

// N: typename, T: used type
#define MAKE_A(N, T)  class N { \
public: N(){} ~N(){}  bool flag; T data; }

MAKE_A(AInt, int);
MAKE_A(AFloat, float);

By using extra implementation-header files:

file: A.h (do not place inclusion guards):

// A_NAME: typename, A_TYPE: used type
class A_NAME { 
public: 
    A_NAME(){} 
    ~A_NAME(){}  
    bool flag; 
    A_TYPE data; 
};

Usage:

#define A_NAME AInt
#define A_TYPE int
#include "A.h"
#undef A_NAME
#undef A_TYPE 

#define A_NAME AFloat
#define A_TYPE float
#include "A.h"
#undef A_NAME
#undef A_TYPE 

One advantage of BOOST.PP over templates is that programs generated with BOOST.PP compile faster than equivalent programs using templates, at least according to this post to the boost devel list.

Even with the improved speed of gcc using hashes instead of linear lookup, I believe using BOOST.PP is still faster, I guess because of the limitations mentioned by Walter Bright in the referenced post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top