Reputation: 139
I have the following class:
struct A
{
template<typename T1, typename T2>
static double constexpr getPercentage(const T1 t1, const T2 t2)
{
return 100.0 * static_cast<double>(t1) / static_cast<double>(t2);
}
};
I have aliased and use A::getPercentage
for B
as follows:
struct B
{
template<typename T1, typename T2>
using getPercentage = decltype(A::getPercentage<T1, T2>);
void f()
{
const double d = getPercentage(1.0, 2.0); //ERROR: Cannot refer to class template 'getPercentage' without a template argument list
}
};
What is wrong with this? How can I avoid this error?
Upvotes: 3
Views: 573
Reputation: 65620
You can't alias functions. Your getPercentage
alias is an alias for the type of A::getPercentage
.
Instead of aliasing A::getPercentage
, you could add a forwarding function:
struct B
{
//forwarding function
template<typename T1, typename T2>
static double constexpr getPercentage(const T1 t1, const T2 t2)
{
return A::getPercentage(t1,t2);
}
void f()
{
//calls B::getPercentage, which calls A::getPercentage
const double d = getPercentage(1.0, 2.0);
}
};
Upvotes: 2
Reputation: 30605
The using
is a type alias. getPercentage
is not a type, it is a function. You cannot alias functions.
Form what your intent looks like, a forwarding function would be more appropriate.
template <typename T1, typename T2>
double constexpr getPercentage(T1&& t1, T2&& t2)
{
return A::getPercentage(std::forward<T1>(t1), std::forward<T2>(t2));
}
For a full listing;
struct A
{
template<typename T1, typename T2>
static double constexpr getPercentage(const T1 t1, const T2 t2)
{
return 100.0 * static_cast<double>(t1) / static_cast<double>(t2);
}
};
struct B
{
template <typename T1, typename T2>
static constexpr double getPercentage(T1&& t1, T2&& t2)
{
return A::getPercentage(std::forward<T1>(t1), std::forward<T2>(t2));
}
void f()
{
const double d = getPercentage(1.0, 2.0);
}
};
Upvotes: 4