0

Playing around trying to make a fraction class, found an apparent glitch. This code shows the problem,

The class is:

template <int A, int B>
struct two_ints
{
    static constexpr int first = A < 100 ? A : -1;
    static constexpr int second = B < 100 ? B : -1;
};


template <typename T>
struct myPair
{
    T one;
    T two;
    template <int A, int B>
    constexpr myPair(two_ints<A, B>)
        : one(two_ints<A,B>::first), two(two_ints<A,B>::second){    }

    template <int A, int B>
    struct make_pair
    {
        static constexpr myPair<T> value = myPair<T>(two_ints<A,B>());
    };
};

When I create an object of the class with "int" as the template perameter it compiles with no problems e.g.

int main()
{
    typedef myPair<long> int_pair;
    int_pair pairTwo(two_ints<67,45>());
    int_pair pairThree(int_pair::make_pair<67,45>::value);

    return 0;
}

But then i uses "long long" as the template parameter and it fails to compile.

int main()
{
    typedef myPair<long long int> int_pair;
    int_pair pairTwo(two_ints<67,45>());
    int_pair pairThree(int_pair::make_pair<67,45>::value); //error here

    return 0;
}

The error is: undefined reference to myPair::make_pair<67, 45>::value' Using compiler: GNU GCC Compiler

Edit: To complicate matters further, if i make value and constexpr function istead of variable then the code compiles...

static constexpr myPair<T> value() { return myPair<T>(two_ints<A,B>()); }

and then in int main()

int_pair pairThree( int_pair::make_pair<67,45>::value() );

This gives no errors at all.

0 Answers0