https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104577
mail at jhellings dot nl changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |mail at jhellings dot nl
--- Comment #2 from mail at jhellings dot nl ---
I looked a bit further into this and into what the standard says. GCC does
partially the correct thing in this case, whereas several other compilers do
the wrong thing. See https://jhellings.nl/article?articleid=1 for the full
analysis.
The short summary:
In Clause 8 of Section [temp.param], the standard defines the value of a
non-type template argument:
"An id-expression naming a non-type template-parameter of class type T denotes
a static storage duration object of type const T known as a template parameter
object, whose value is that of the corresponding template argument after it has
been converted to the type of the template-parameter. ..."
Hence, whatever is provided as a non-type template parameter argument (of type
S in this bug report) is converted to the type S and the value resulting from
this conversion is available within the template as an lvalue object of type
const S.
To convert an expression to type S, you either need a constexpr copy
constructor (general case) or a constexpr move constructor (in the special case
in which you provide a movable value).
Note that both Clang and Microsoft C++ do not correctly implement the semantics
of non-type template parameters (they pass values without converting them to
the type of the non-type template parameter).
I did find a separate issue, however:
/*
* @author{Jelle Hellings}.
* @copyright{The 2-Clause BSD License; see the end of this article}.
*/
/*
* A type that can only be default-constructed and moved.
*/
struct no_copy
{
/*
* We can default-construct a dummy.
*/
constexpr no_copy() {};
/*
* We cannot copy dummy.
*/
no_copy(const no_copy&) = delete;
/*
* But we certainly can move a dummy.
*/
constexpr no_copy(no_copy&&) {}
};
/*
* A template function that accepts a no_copy non-type template parameter, but
* does not do anything with it.
*/
template<no_copy NC>
void test_f()
{
/* We cannot pass NC to another template, as we do not have a copy
* constructor. We can use this template by moving in a no_copy, however.
*/
};
/*
* A template struct that accepts a no_copy non-type template parameter, but
* does not do anything with it.
*/
template<no_copy NC>
struct test_t
{
/* We cannot pass NC to another template, as we do not have a copy
* constructor. We can use this template by moving in a no_copy, however.
*/
};
/*
* Entry-point of the program.
*/
int main ()
{
test_f<no_copy{}>(); // Works fine, as it should.
test_t<no_copy{}> value; // <- error: use of deleted function.
}