I want to create template class to get reference type and non-reference type template class with same class like below
template <typename T> requires (std::is_reference_v<T>)
struct RRR
{
T m_value{};
RRR(T init) : m_value{ init }
{}
};
template <typename T> requires (!std::is_reference_v<T>)
struct RRR
{
T m_value{};
RRR(T init) : m_value{ init }
{}
};
but when I use this, I met below compile error.
error: redeclaration ‘template requires is_reference_v struct RRR’ with different constraints
what is solution for defining template like this case?
Make them partial specializations:
template <typename T>
struct RRR;
template <typename T> requires (std::is_reference_v<T>)
struct RRR<T>
{
T m_value{};
RRR(T init) : m_value{ init }
{}
};
template <typename T> requires (!std::is_reference_v<T>)
struct RRR<T>
{
T m_value{};
RRR(T init) : m_value{ init }
{}
};
You can even have one of them be a partial specialization and the other the primary template:
template <typename T>
struct RRR
{
T m_value{};
RRR(T init) : m_value{ init }
{}
};
// This one will be used for references since it is more constrained
template <typename T> requires (std::is_reference_v<T>)
struct RRR<T>
{
T m_value{};
RRR(T init) : m_value{ init }
{}
};