Thread: Initialization of a static const class member at compile time

  1. #1
    Registered User
    Join Date
    Aug 2010

    Initialization of a static const class member at compile time

    Hello all! I'm working with circuitbreaker on the same topic of "Expression Templates" and "Template Meta-Programming" for arrays. (see post "Basic doubt about expression template")

    About the aspect of "template meta-programming". With the idea of being able to compute something at compile-time, I am looking at a way to create the specific variation of the class, instantiate an object of that class and initialize its content (the values) at compile-time.

    The specific variation of the class is created at compile-time.
    An actual object, an instance of a matrix class and its storage, is not created until run-time.
    I have the idea that if I could do that, I could then use "Expression Templates" in order to handle those objects and all this at compile-time.

    The first question is:
    1) Does that sound irrealistic or am I somehow on a good path?

    Then, the problem seems to be the initialization of the object. I thought about passing the values as template parameters:

    template<typename T, unsigned int rows_arg, unsigned int columns_arg, T mat_arg[rows_arg][columns_arg]>
    class Matrix{
         static const unsigned int rows=rows_arg, columns=columns_arg;
         static const T* mat_data = mat_arg;
    int main(){
         Matrix<int, 2, 2, {{3, 5 }, {7, 9 }}> m1;
         return 0;
    But it seems this is not correct. So my second question is:
    2) Is it possible somehow to pass an array of dimension 2 as a template parameter?

    3) If not, is there any other way to initialize the static const member of a template class object?

    Thank you for your help.

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    I doubt it's possible to pass an array at compile-time. AFAIK, it only works for integral types, such as int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2010
    I'm a bit confused on what you are going to achieve.
    instantiate an object of that class and initialize its content (the values) at compile-time.
    You want to pass some constant values via arrays, and then, at compile time, make specific computation on these? How do you want to do that if the array's size may vary?

    Haven't read your previous posts, maybe consider making it at run time and pass pointer to an array? From my point of view it just looks like it is not worth effort to compute it at compile time.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008

    I have reasons for why I did not address the purely "template meta-programming" possibilities of this on the other thread.


    I'm not going to waste time going in to a lot detail. I will only say that calculating mathematics at compile-time is a wonderful thing, but only if that value serves a specific purpose at compile-time. If the computed value is ultimately only ever used at run-time, you need to use a different approach.

    I personally guarantee you that a compiler will perform these types of complex calculations built from templates, when it can perform them at all, slower than any decent run-time construct designed to perform the same calculations using the same algorithms. The template mechanism is incredibly slow, and many compilers will crash when confronted with complex systems of mathematics expressed as recursive templates. (At larger numbers or operations, every compiler will fail or refuse to compile the source.) I would estimate that you would be allowed and allow no more than a few dozen operations on "4x4" matrices of integer values before the compiler failed or you got tired of waiting every time you needed to recompile. (If you need a "real" value, you may as well stop now, building a facility to express floating point values at compile-time is possible, but it will increase the time it takes to compile the unit by more than you will imagine.)

    If, and only if, you need to compute a value to be used at compile-time, you can eventually get a working system to produce a compile-time value using "template meta-programming". You must ignore all constructs not built from purely compile-time constructs. You must forget about "expression templates". (The technique is simply not useful in this case.) You must build every primitive you will need using templates to express: constant values, types, types yielding constant values, "getters and setters" to manipulate these values, and any relevant data structures (including arrays*). You must be ready to implement all of this with "templates express immutable constructs" in mind. (You can not, for example, modify one of the "array" values; you must copy the "array" while replacing the relevant value.) You must only use compile-time constructs.

    I strongly encourage you to pursue this further, but only as a learning exercise. It will likely teach you more than you wished to know--and far more than may be useful.


    * The arrays native to C++ are compile-time syntactical sugar for a run-time construct.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    You might as well create your own commented scripting language and run your sources through your own preprocessing program if you're interested in having short-hands for hard-coded compile-time stuff. You could even include that code in your project and script the process into your IDE.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM