CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    75

    Function Templates

    Well, I think I am stuck:
    Code:
    template<int TSIZE>
    struct A
    {
    	int get_size() const { return TSIZE; };
    	char buffer[TSIZE];
    };
    
    template<int TSIZE>
    void Foo()
    {
    	char buffer[TSIZE];
    	std::cout << TSIZE << std::endl;
    }
    
    int main(int argc, char* argv[])
    {
    	A<12> a12;
    	std::cout << a12.get_size() << std::endl; // outputs 12, ok
    
    	A<5> a5;
    	std::cout << a5.get_size() << std::endl; // outputs 5, ok
    
    	Foo<12>(); // outputs 5, WHY???
    	Foo<5>(); // outputs 5, ok
    
    	return 0;
    }
    Can somebody explain why Foo<12>(); outputs 5 and not - as I expected - 12?

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Thumbs up Re: Function Templates

    in your protgram if u comment last line Foo<5> you can see now u r getting F<12> value to 12 Only
    But at the time when u uncomment follwong line you will see that due to last function first is also changing it's value if u write any third Function
    like Foo<15>() u will see now all three having value of 15.in you First two call you are calling your structure member function get_size
    so it's printing 12 & 5 properly but in your Function call both are taking same address space in memory so changing ones will affect the value of both
    that's why when u r Printing individual value u will get right result but in case of 2 or more what is the last value that will replaced all previos value

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Function Templates

    This is actually a bug with the VC++ 6.0 compiler that has been fixed in the version 7 and I guess you are working with this compiler. I tried your code with gcc and it works as expected. VC++ 6.0 seems to ignore the template arguments in this case and distinguishes the functions based on their function arguments. Here is a FAQ regarding this at C++ Templates FAQ. Look at FAQ 5. Hope this helps. Regards.

    EDIT: You could try using std::string instead of char buffers or else you would need to make dynamic allocations using new() and then call delete to maintain char buffer.
    Last edited by exterminator; September 13th, 2005 at 04:13 AM.

  4. #4
    Join Date
    May 2004
    Posts
    75

    Re: Function Templates

    @humptydumpty
    Thanks for your comment. I also realized this behavior, since I am playing around with this issue quite a few days. The question was *why* both function calls have the same address.

    @exterminator
    You guessed right... (I can not upgrade because my workgroup stick with it). I had the strong feeling that I wrote something like this before and it worked, but it was most probably at home on my VC7.1 compiler

    Thanks for the link and for your suggestion, too. I actually do not need to reserve a buffer at all, it was just for demonstration. What I really wanted to do in the actual product is to use a member function as a template argument of a make_xxx function that creates a templated class object that in turn calls the passed member function later on. Everything worked fine without the make_xxx function, so I started a simple demo project to play around with it.

    Now, I'll have to find a different solution... Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured