Vincent
Vincent

Reputation: 60381

Alias template specialization issue?

Consider this code:

#include <iostream>
#include <ratio>

template <class R1, class R2, class... RN> 
using ratio_sum = ratio_sum<ratio_sum<R1, R2>, RN...>;

template <class R1, class R2>
using ratio_sum<R1, R2> = std::ratio_add<R1, R2>;

int main()
{
    std::cout<<ratio_sum<std::ratio<3>, std::ratio<2>>::num<<std::endl;
}

It crashes with the following error:

main.cpp:5:23: error: expected type-specifier before 'ratio_sum'
using ratio_sum = ratio_sum<ratio_sum<R1, R2>, RN...>;

How to solve this problem ? (As it illustrates a generic problem, I dont want a workaround that uses std::ratio_add in the variadic version).

Upvotes: 0

Views: 157

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

You have two errors in your code. The first, which you get, is that ratio_sum<R1, R2> is not defined when you try to use it. The solution to this is simple: Switch place of the two definitions.

The second problem is that you can not use templates when defining a type alias in the case of ration_sum<R1, R2>.

Upvotes: 1

Related Questions