C++ Logo

std-discussion

Advanced search

Re: Partial specialization with only one template parameter in the primary template.

From: Andrew Schepler <aschepler_at_[hidden]>
Date: Sat, 28 Sep 2019 10:24:54 -0400
Not sure I understand. When you want to define one specific specialization
like A<10>, that's what an explicit specialization is for. A partial
specialization is useful when you want to define the template for some
larger subset of the possible template arguments.

Though C++20's constraints and concepts will actually make it possible to
do partial specializations of a template like your A (without changing A to
add another dummy template parameter, like the "template <int N, typename
Enable=void>" technique). For example, to define A for all argument values
10 or larger:

namespace N {
template <int N> requires (N >= 10)
struct A<N> {
  A() { std::cout << "A<N>() [N>=10]\n\n"; }
};
}


On Sat, Sep 28, 2019, 9:29 AM Vladimir Grigoriev via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> In the Standard in teh section devoted partial specialization there is
> nothing said about cases when a primary template has only one template
> parameter. In this case sometimes it is impossible to declare a partial
> specialization and instead of a partial specialization we have an explicit
> specialization.
>
> For example
>
> #include <iostream>
>
> namespace N
> {
> template <int>
> struct A
> {
> A() { std::cout << "A<int>()\n" << '\n'; }
> };
> }
>
> using N::A;
>
> namespace N
> {
> template <>
> struct A<10>
> {
> A() { std::cout << "A<10>()\n" << '\n'; }
> };
> }
>
> int main()
> {
> A<1> a1;
> A<10> a2;
> }
>
> How to define a partial specialization for template argument 10?
>
> So I think the description of the section should point out such cases.
>
> With best regards,
> Vlad from Moscow
>
>
>
> You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or
> http://ru.stackoverflow.com
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2019-09-28 09:27:17