tags:

views:

170

answers:

3

In this code (ready to compile):

      #include "stdafx.h"
        #include <iostream>
        #include <sstream>

        using std::cout;

        template<class T, int first, int second>
        T make()
        {
            T result = T();
            std::stringstream interpreter;
            interpreter << first << '.' << second;
            interpreter >> result;
            return result;
        }

 template<int first, int second, class T = double>
    struct Make
    {
        typedef T value_type;
        static value_type value;

    };

    template<int first, int second, class T>
    T Make<first,second,T>::value = make<T,first,second>();



    template<int first, int second>
    struct Real
    {
        typedef double type;
        static type value;
    };

        template<int first, int second>
    typename Real<first,second>::type typename Real<first,second>::value = typename Make<first,second>::value;  


       int _tmain(int argc, _TCHAR* argv[])
    {
        //cout << Make<1,2>::value << '\n';//UNCOMMENT THIS AND SEE WHAT I MEAN
        cout << Real<1,2>::value;
        return 0;
    }

Please see the comment 4 lines above.

+1  A: 

This works for me with two tweaks, remove the redundant typename decelerations:

template<int first, int second>
typename Real<first,second>::type typename Real<first,second>::value = typename Make<first,second>::value;  

becomes:

template<int first, int second>
typename Real<first,second>::type Real<first,second>::value = Make<first,second>::value;

(at least in gcc 4.4.4)

The result is 1.2, 1.2 - which is as expected(?)

Nim
@Nim but did you try to compile the code with just Real<1,2>::value?
There is nothing we can do
When I comment the first line in main() I get 0. If I leave both, I get 1.2 1.2
Armen Tsirunyan
Nim
@Nim: No one is charging you, no need to defend, I just wanted to ask you what you get when you delete first line. :) So, what do you get?
Armen Tsirunyan
Anyways, on gcc this works fine... would imply then as Armen says above, this is an issue with your compiler...
Nim
@Nim: Upvoted back because that really wasn't deserved...
ereOn
@ereOn: Thanks!
Nim
@Nim so if you have just Real<1,2>::value you get on console 1.2? using gcc?
There is nothing we can do
@There, yes, I get 1.2
Nim
+4  A: 

That isn't ready to compile (you don't use typename where you expect a variable name). After fixing those things, I get 1.2 for both:

http://codepad.org/z3UCiOfK

http://codepad.org/66xnnLbd

Edit: It didn't work in VS 2005. This must be a problem in VC++ (at least in 2005). It's probably related to how they do certain template processing later than the standard requires. That's just a guess, though.

Steve M
@Steve M thanks for a correction.
There is nothing we can do
Why did you downvote me?
Steve M
I didn't downvote you, but my guess is the codepad link. Just paste code into the answer -- SO has pretty good code viewing.
Lou Franco
I've edited with more info after I tried it in VS 2005. So you aren't totally crazy.
Steve M
@Lou: Yeah, but SO doesn't run the code and show the output like codepad. The output is the import part here.
Steve M
+2  A: 

If you call Real<1,2>::value before you call Make<1,2>::value, it gets initialized first, so it gets Make<1,2>::value's initial non-initialized value, which is 0.

If you call Make<1,2>::value first, it gets initialized properly with the make() function, it gets the value 1.2. Then, since Real<1,2>::value gets initialized afterwards, it gets that value.

PigBen