public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mail at jhellings dot nl" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/104577] needs copy constructor for class non-type template parameter
Date: Mon, 26 Dec 2022 04:19:43 +0000	[thread overview]
Message-ID: <bug-104577-4-89jFr656Ts@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104577-4@http.gcc.gnu.org/bugzilla/>

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.
}

  parent reply	other threads:[~2022-12-26  4:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-17  3:55 [Bug c++/104577] New: needs copy constructor to call method of " f.heckenbach@fh-soft.de
2022-02-18  1:14 ` [Bug c++/104577] needs copy constructor for " pinskia at gcc dot gnu.org
2022-12-26  4:19 ` mail at jhellings dot nl [this message]
2022-12-28  1:30 ` f.heckenbach@fh-soft.de
2023-02-11  4:35 ` herring at lanl dot gov
2023-06-05 19:29 ` ppalka at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104577-4-89jFr656Ts@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).