Can not-const nullptr_t variable be a template non-type argument?

5 days ago 4
ARTICLE AD BOX

Yes: the resolution of CWG2907 made this code valid by specifying that the lvalue-to-rvalue conversion can be applied to any expression of type cv nullptr_t during constant evaluation, even if the underlying object is not usable in constant expressions.

duck's user avatar

Yeah GCC/Clang are right — since C++17 you can totally use a constexpr nullptr_t variable as a nontype template arg when the parameter is decltype(nullptr) (or auto in C++17+).

MSVC is just behind on this one (still rejects it in 2025/2026 main branch), EDG is being extra strict / old-school about constant expressions here.

Quick thing that usually works everywhere now:

template<auto P> inline constexpr int v = 0;

constexpr auto nil = nullptr;

static_assert(v<nil> == 0); // accepted by gcc/clang/msvc 2025+

So your original example with auto nil = nullptr; is actually standard-conforming (as long as you use decltype(nullptr) or auto for the template param).

MSVC bug → give them a few more months/years ¯\_(ツ)_/¯

that's it

Gerdona Mendez's user avatar

1 Comment

Why repeat the same things when it is already mentioned by another answer. This adds nothing to the already existing answer. This even lacks standard reference as this is a language lawyered question.

2026-01-31T06:29:04.02Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article