C++ Logo

std-discussion

Advanced search

Re: Non-type template argument issue

From: Andrey Davydov <andrey.davydov_at_[hidden]>
Date: Tue, 9 Feb 2021 09:06:22 +0300
On Wed, Feb 3, 2021 at 8:36 PM Edward Diener via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> Furthermore in the situation above the only way that the code could
> handle the possibility of a nullptr being passed as an argument for "T *
> t" in the code of the primary template would be run-time based...,

It could be expressed in C++20 using requires clause (
https://gcc.godbolt.org/z/Y66qz4):

template <class T, T * t>
class ATemplate { /* some code... */ };

template <class T, T * t> requires (t == nullptr)
class ATemplate<T, t> { /* some code... */ };


> where clearly I might want to provide a partial specialization so that
> member
> functions in the primary template where a nullptr based 't' are
> incorrect could be removed completely. In other words I would very much
> like to handle a nullptr based 't' at compile time rather than at
> run-time, and I see no way to do this.
>
Or maybe even more suitable in your case don't create class specialization
at all (https://gcc.godbolt.org/z/9oPWGK):

template <class T, T * t>
class ATemplate {
    static constexpr bool t_is_not_null = t != nullptr;

    void f() requires t_is_not_null { /*...*/ }
};

-- 
Andrey Davydov

Received on 2021-02-09 00:06:36