Boost logo

Boost :

From: Mr.ZAUJ (dansei_at_[hidden])
Date: 2003-05-22 19:40:34


Hi, all!

I think we've reached the limitation of C++ template!
The code your guys written is really complex, but is that worth so 'complex'? or, say, do we must code the general functions in such complex way?

We can invent a new style general programming language, it may be a preprocess language, or something mixed, like 'Perl'. Then a compiler can translate that language to C/C++ (well, I can prove that).

I have some ideas:
    1) e.g. vc++ 6 has a C4786 warning, it is a potential error when compiling a complex template, #pragma warn(disable:4786) isn't an ideal solution. The only way is preprocess the code to generate instantiated template functions or classes, like this:

        template<class t> t max_a_reallyloooo~~ooongname(const t, const t);

      after instantiated:

        int max_a_reallyloooo~~ooongname_int(int, int);
        int max_a_reallyloooo~~ooongname_float(float, float);

      then, the preprocessor found this name is too long, so it can rename that to:

        int max_a_re5783985(int, int);
        int max_a_re5782745(float, float);

    2) A more smart data layout structure. I found many library do not implement the basic data type very well. we can write a counted string as this:
        struct counted_string {
            int nLength;
            char the_first_char[1];
        };
      then allocate a string as counted_string *hello = (counted_string *)(new BYTE[12+sizeof(counted_string)];
            strcpy(hello->the_first_char, "hello,world!");
      yes, we still can do whatever, if we'd please to write lots of complex code. But in this example, unless we change "char the_first_char[]" to a pointer as "char *pStr", which will waste 4 bytes more(not all examples waste only 4-bytes), I really can't find a better way. We can use template, like:
        template<int n> {
            struct counted_string {
                int nLength;
                char the_content[n];
            };
        };
      but do you agree that "a 3-chars long string is of a different type from 4-chars long string"?
      

    3) The most important in my personal opinions, which is about the runtime architecture. Because template is a compile-time idea, when it is running, e.g. an instantiated template-struct has no information about its type. we must provide a runtime type information, so some other stand-alone programs can know what data is transfered in. The variant, which always have a type-id at the beginning of binary data, won't face such problem. We still can use C++ RTTI or by some static members, to make something has type info, but it is not a "very good" way. In fact, I was designing something like 'boost::variant', but I found I can't design it well, anyway. I think, we've reached the limit of C++ template!

/zauj.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk