max value of instantiated template function

This is a discussion on max value of instantiated template function within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm wondering if anyone has any tricks to determine the maximum instantiated value for a template function. For exampe, ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    max value of instantiated template function

    Hi,

    I'm wondering if anyone has any tricks to determine the maximum instantiated value for a template function. For exampe, how would one modify the following code without changing main, to make MAXV a valid number:

    Code:
    #include <stdio.h>
    template<int V> 
    void test(){
        printf("V: %d\n",V);
    }
    
    int main(){
        test<2>();
        test<6>();
        test<2>();
        test<3>();
        
        printf("maxV: %d\n",MAXV);
        return 0;
    }

    Thanks for the help!

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    one more thing

    I need to know MAXV at compile time!

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Might not be possible.

    And what about:
    Code:
    int main(){
        printf("maxV: %d\n",MAXV);
        test<2>();
        test<6>();
        test<2>();
        test<3>();
       
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Yes, ideally that would work as well. I was hoping some template or pre-processor magic could pull it off.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    The only way it *might* be possible that I can think of is a recursive solution that takes in all V values at once and finds the result and returning it.
    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.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Simple:
    Code:
    #include <algorithm>
    #include <stdio.h>
    
    int MAXV = 0;
    
    template<int V> 
    void test(){
        MAXV = std::max( MAXV, V );
        printf( "V: %d\n", V );
    }
    
    int main(){
        test<2>();
        test<6>();
        test<2>();
        test<3>();
        
        printf( "maxV: %d\n", MAXV );
        return 0;
    }
    Although, I don't think this will work at compile time, just run time.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Unfortunately, that solution does not satisfy the requirement:
    Quote Originally Posted by npackard View Post
    I need to know MAXV at compile time!
    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.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think that requirement is impossible to satisfy. Basically you'd have to create a compile time constant from code that the compiler hasn't seen yet. (Not that is particularly easy to gather compile-time data from instantiation of function templates.)

    It would be interesting to know, why OP needs something like that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yeah, I'm wondering why you'd need to know that at compile time?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Basically I am a student and have implemented an N-Dimensional Image Class with various image processing routines implemented in ND that I use for medical image processing. I want to implement a generic struct that can hold information about any instantiation of my image class. This struct resides in shared memory to provide information to a separate process that runs a GUI to view the ND data.

    For example, the struct looks something like:

    Code:
    struct ViewInfo{
        int nDIM;
        char dataname[sizeof(void*)+1]; //stores an name to access raw data from shared memory
        int datatype; //stores info about scalar type
        int ndim; //number of dimensions
        int ncolors; //number of colors
        int dim[MAXN]; //coordinates of the ND pixel
        float size[MAXN]; //dimensions (in mm) of the ND pixel
    };
    My image class takes in as template parameters the scalar type (i.e. float, int, short, etc) and the dimensionality (i.e. N=1,2,3, or 5000 for example). The advantage to knowing the dimensionality at compile time is that it allows for optimizations of my ND image processing routines. The class looks like:

    Code:
    template<class VoxelType,int N>
    class Image {
    public:
    //...
    };
    I want to create a generic structure that holds information about each instantiation of the image class and resides in shared memory. I can't use dynamic allocation because it gets kind of messy dynamically allocating in shared memory. I could just "#define MAXN 100" for example, but I would rather not limit the max value of N and would rather determine MAXN at compile time depending on the program that is using the library.

    I realize that my request is a bit demanding, but if I could figure this out it would make for a much more elegant solution that I could otherwise come up with.

    Thanks

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Instead of a define you can use a constant, and each instantiation of anything can assert at compile-time that it's N size is less-or-equal to that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    That's a good suggestion Anon, thanks for a workable solution. Maybe my original request is impossible.

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. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21