简体   繁体   中英

How to do partial template class specialization with concept and requires?

It's easy to write template function override with concept, but I don't know how to write template class partial specialization:(

template <typename T>
concept Integral = is_integral_v<T>;

template <typename T>
concept IsNotIntegral = !
is_integral_v<T>;

template <typename T>
class Test
{
};

template <Integral T> // wrong
class Test
{
};

template <typename T> // wrong
    requires Integral<T>
class Test
{
};

int main()
{
    Test<int> t;
}

This doesn't work either:(

template <Integral T>
class Test
{
};

template <IsNotIntegral T>
class Test
{
};

Both

template <Integral T> // wrong
class Test
{
};

and

template <typename T> // wrong
    requires Integral<T>
class Test
{
};

are using the declaration syntax of a primary class template , not a partial specialization. Compare the syntax of an unconstrained partial specialization from before concepts:

// primary class template
template <typename T>
class Test
{
};

// partial specialization of the class template
template <typename T>
class Test<T*>
{
};

The difference is that there isn't just a class name in the partial specialization after the class keyword, but a template-id Test<T*> instead. Without it a partial specialization makes no sense since we wouldn't know what template arguments we are specializing for.

So with constraints the syntax for a partial specialization should be the same:

template <Integral T>
class Test<T>
{
};

or

template <typename T>
    requires Integral<T>
class Test<T>
{
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM