public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/63924] New: Constexpr constructible expression "is not constexpr" when used in a template non-type argument
@ 2014-11-18  1:09 mouchtaris at gmail dot com
  2014-11-19  2:15 ` [Bug c++/63924] " jason at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: mouchtaris at gmail dot com @ 2014-11-18  1:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63924

            Bug ID: 63924
           Summary: Constexpr constructible expression "is not constexpr"
                    when used in a template non-type argument
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mouchtaris at gmail dot com

A proper constexpr constructible type is found non-constexpr by the compiler,
under the following conditions:

- it is used in an expression which is passed as a template non-type argument,
- its copy constructor is defaulted (whether explicitly or implicitly).

In the following test case this is demostrated:

// -------------------------------------------------------
// File t.cpp
// -------------------------------------------------------
// utils
template <unsigned N> struct require_constexpr {
  static constexpr unsigned value = N;
};
template <typename...> constexpr void noop (void) { }

// a constexpr constructible class
struct test { 
  constexpr unsigned size() const { return 0; }
  constexpr test() { }
  constexpr test(const test &) = default;
};

// size wrappers
constexpr auto size0 (test t) { return t.size(); }
// just making sure type "test" is still considered
constexpr auto size1 (test t) { return size0(t); }
// constexpr constructible outside a template argument
constexpr auto size2 (test t) { return size1(t); }
// PROBLEM here
constexpr auto size3 (test t) { return require_constexpr< size0(t) >::value; }

int main (int, char**)
{
  constexpr auto const ar = test { };

  noop<
    require_constexpr< size3(ar) >
  >();

  return 0;
}
// -------------------------------------------------------


Compiling with
    g++ -std=c++1y -pedantic -Wall -Wextra -o /tmp/a t.cpp 

and here is the output:
//--------------------------------------------------------
t.cpp: In function ‘constexpr auto size3(test)’:
t.cpp:22:68: error: ‘t’ is not a constant expression
 constexpr auto size3 (test t) { return require_constexpr< size0(t) >::value; }
                                                                    ^
t.cpp:22:68: note: in template argument for type ‘unsigned int’ 
t.cpp:22:16: error: invalid return type ‘void’ of constexpr function ‘constexpr
auto size3(test)’
 constexpr auto size3 (test t) { return require_constexpr< size0(t) >::value; }
                ^
t.cpp: In function ‘int main(int, char**)’:
t.cpp:29:34: error: could not convert template argument ‘size3((ar, test()))’
to ‘unsigned int’
     require_constexpr< size3(ar) >
                                  ^
t.cpp:30:5: error: no matching function for call to ‘noop()’
   >();
     ^
t.cpp:30:5: note: candidate is:
t.cpp:6:39: note: template<class ...> constexpr void noop()
 template <typename...> constexpr void noop (void) { }
                                       ^
t.cpp:6:39: note:   template argument deduction/substitution failed:
t.cpp:30:5: error: template argument 1 is invalid
   >();
     ^
//--------------------------------------------------------

"test" is still properly constexpr-copy-constructed when used outside
template arguments, as in size1() and size2().

The code segment also compiles cleanly if we define a non-default constructor.
In other words, if we change line
  constexpr test(const test &) = default;
to
  constexpr test(const test &) { }

So the bug (probably) pertrains to defaulting constructors and the use of such
in non-type template arguments.
>From gcc-bugs-return-467099-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue Nov 18 01:11:09 2014
Return-Path: <gcc-bugs-return-467099-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 15776 invoked by alias); 18 Nov 2014 01:11:08 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 15713 invoked by uid 48); 18 Nov 2014 01:10:59 -0000
From: "mouchtaris at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/63924] Constexpr constructible expression "is not constexpr" when used in a template non-type argument
Date: Tue, 18 Nov 2014 01:11:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c++
X-Bugzilla-Version: 4.9.2
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: mouchtaris at gmail dot com
X-Bugzilla-Status: UNCONFIRMED
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields:
Message-ID: <bug-63924-4-Y43lGw1XAR@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-63924-4@http.gcc.gnu.org/bugzilla/>
References: <bug-63924-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-11/txt/msg01571.txt.bz2
Content-length: 224

https://gcc.gnu.org/bugzilla/show_bug.cgi?idc924

--- Comment #1 from Nikos <mouchtaris at gmail dot com> ---
Special thanks and kudos to "T.C." for helping figuring this out:

http://stackoverflow.com/questions/25465379


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-11-19 22:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-18  1:09 [Bug c++/63924] New: Constexpr constructible expression "is not constexpr" when used in a template non-type argument mouchtaris at gmail dot com
2014-11-19  2:15 ` [Bug c++/63924] " jason at gcc dot gnu.org
2014-11-19  3:04 ` jason at gcc dot gnu.org
2014-11-19  3:05 ` jason at gcc dot gnu.org
2014-11-19 22:15 ` mouchtaris at gmail dot com
2014-11-19 22:25 ` jason at gcc dot gnu.org

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