incrementing an integer at compile time, across source files

Discussion in 'C++' started by Sean Dettrick, Aug 7, 2003.

  1. I am wondering if it is possible to increment an integer at compile
    time using template<int N> (or by any other method, e.g. compiler
    directives etc), across multiple source files.

    This may sound crazy, but I wish to build a string array (which
    requires no user maintenance) of source file RCS version numbers!
    The idea is, I would like to be print the beggers out or operate on
    them at run time if I feel like it, by using a (maintenance free) loop.
    By maintenance free, I mean that in each source file I would like to
    be able to refer to some integer, say RCS::N, which is automatically
    different in every source file, without the user having to specify
    it (except for an initial value).

    i.e. I would like to be able to have a set of files like this:

    // rcs.h:
    #include <string>
    namespace RCS{
    const int Nfiles_max=20; // max number of source files
    string[Nfiles_max] Version; // to hold RCS version numbers of source files
    int N;

    template<int N> some_handy_way_to_increment_N_at_compile_time{};
    };

    // fileA.cpp:
    // if fileA.cpp is compiled first, want RCS::N to have the value 0.
    #include "rcs.h"
    RCS::Version[RCS::N] = "$Revision$"
    /* ... stuff ... */

    // fileB.cpp:
    // if fileB.cpp is compiled second, want RCS::N to have the value 1.
    #include "rcs.h"
    RCS::Version[RCS::N] = "$Revision$"
    /* ... different stuff ... */

    // fileC.cpp:
    // if fileC.cpp is compiled third, want RCS::N to have the value 2.
    #include "rcs.h"
    RCS::Version[RCS::N] = "$Revision$"
    int main(){

    for (int i=0; i<RCS::Nfiles_max; i++ ) cout << RCS::Version << endl;

    if ( RCS::Version[0] != RCS::Version[1] ) cout << "version mismatch!" << endl;

    }


    Any ideas on this?
    I presume it is impossible, but hope to be pleasantly surprised.

    Thanks in advance,
    Sean Dettrick
     
    Sean Dettrick, Aug 7, 2003
    #1
    1. Advertising

  2. On 6 Aug 2003 16:46:54 -0700, (Sean Dettrick) wrote:

    >I am wondering if it is possible to increment an integer at compile
    >time using template<int N> (or by any other method, e.g. compiler
    >directives etc), across multiple source files.
    >
    >This may sound crazy, but I wish to build a string array (which
    >requires no user maintenance) of source file RCS version numbers!
    >The idea is, I would like to be print the beggers out or operate on
    >them at run time if I feel like it, by using a (maintenance free) loop.
    >By maintenance free, I mean that in each source file I would like to
    >be able to refer to some integer, say RCS::N, which is automatically
    >different in every source file, without the user having to specify
    >it (except for an initial value).
    >
    >i.e. I would like to be able to have a set of files like this:
    >
    >// rcs.h:
    >#include <string>
    >namespace RCS{
    > const int Nfiles_max=20; // max number of source files
    > string[Nfiles_max] Version; // to hold RCS version numbers of source files
    > int N;
    >
    > template<int N> some_handy_way_to_increment_N_at_compile_time{};
    >};
    >
    >// fileA.cpp:
    >// if fileA.cpp is compiled first, want RCS::N to have the value 0.
    >#include "rcs.h"
    >RCS::Version[RCS::N] = "$Revision$"
    >/* ... stuff ... */
    >
    >// fileB.cpp:
    >// if fileB.cpp is compiled second, want RCS::N to have the value 1.
    >#include "rcs.h"
    >RCS::Version[RCS::N] = "$Revision$"
    >/* ... different stuff ... */
    >
    >// fileC.cpp:
    >// if fileC.cpp is compiled third, want RCS::N to have the value 2.
    >#include "rcs.h"
    >RCS::Version[RCS::N] = "$Revision$"
    >int main(){
    >
    > for (int i=0; i<RCS::Nfiles_max; i++ ) cout << RCS::Version << endl;
    >
    > if ( RCS::Version[0] != RCS::Version[1] ) cout << "version mismatch!" << endl;
    >
    >}
    >
    >
    >Any ideas on this?
    >I presume it is impossible, but hope to be pleasantly surprised.


    It's not impossible, but very labor-intensive: you have to write your
    own compiler, unless you can find one that supports what you think you
    need (a typical compiler retains no memory from one compilation to
    the next).

    What you think you need is not what you need.

    Also, the assumption of identical revision numbers for compatible
    source files is probably unfounded.

    <ot>
    One solution to what appears to be the real problem is to use a source
    control system + agreed on rules for checking in and checking out.
    </ot>
     
    Alf P. Steinbach, Aug 7, 2003
    #2
    1. Advertising

  3. On Thu, 07 Aug 2003 00:06:24 GMT, (Alf P. Steinbach) wrote:
    ><ot>
    >One solution to what appears to be the real problem is to use a source
    >control system + agreed on rules for checking in and checking out.
    ></ot>


    Agreed, this seems to be a problem best solved by your version control system.
    Try reading up on automatically-expanded version contral tags that you can
    embed in your source code (such as $Revision$, etc.) to see if they suit your
    needs.
     
    Dave Rahardja, Aug 7, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Stockwell

    incrementing a time tuple by one day

    David Stockwell, Sep 23, 2004, in forum: Python
    Replies:
    5
    Views:
    486
    Cameron Laird
    Sep 23, 2004
  2. Guest
    Replies:
    3
    Views:
    345
    Guest
    Feb 13, 2007
  3. Nagaraj
    Replies:
    1
    Views:
    876
    Lionel B
    Mar 1, 2007
  4. Carter
    Replies:
    2
    Views:
    510
    Carter
    Mar 4, 2009
  5. fAnSKyer
    Replies:
    2
    Views:
    536
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page