public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const
@ 2012-12-12 13:37 daniel.kruegler at googlemail dot com
  2012-12-14 21:36 ` [Bug c++/55663] " mlopez at cse dot tamu.edu
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-12-12 13:37 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

             Bug #: 55663
           Summary: [C++11] Alias template combined with constexpr
                    function is considered non-const
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: daniel.kruegler@googlemail.com


Using gcc 4.8.0 20121209 (experimental) and compile with flags

-Wall -std=c++11 -pedantic

rejects the following program:

//-------------------------------
template<bool, class>
struct enable_if {};

template<class T>
struct enable_if<true, T> { using type = T; };

template<bool C, class T>
using req = typename enable_if<C, T>::type;

template<class T>
constexpr bool always() { return true; }

template<class T>
typename enable_if<always<T>(), void>::type
foo1(T){}

template<class T>
req<always<T>(), void>
foo2(T){}

int main() {
  foo1(0); // OK
  foo2(0); // Error #23
}
//-------------------------------

the diagnostics being:

"main.cpp||In function 'int main()':|
main.cpp|23|error: no matching function for call to 'foo2(int)'|
main.cpp|23|note: candidate is:|
main.cpp|19|note: template<class T> req<always<T>(), void> foo2(T)|
main.cpp|19|note:   template argument deduction/substitution failed:|
main.cpp|19|  required by substitution of 'template<class T> req<always<T>(),
void> foo2(T) [with T = int]'|
main.cpp|23|required from here|
main.cpp|19|error: integral expression 'always<int>()' is not constant|
main.cpp|19|error:   trying to instantiate 'template<bool C, class T> using req
= typename enable_if::type'|
"

I found some similarity to bug 54648 but I'm not 100% sure whether this is a
dub


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
@ 2012-12-14 21:36 ` mlopez at cse dot tamu.edu
  2012-12-14 22:08 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mlopez at cse dot tamu.edu @ 2012-12-14 21:36 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Michael Lopez <mlopez at cse dot tamu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mlopez at cse dot tamu.edu

--- Comment #1 from Michael Lopez <mlopez at cse dot tamu.edu> 2012-12-14 21:35:17 UTC ---
I also ran into this bug using GCC 4.8.0 20121121 (experimental) with compile
flags

-std=c++11 -Wall -pedantic -Wextra

Here is a simpler version of the reported bug.

template <typename>
constexpr bool the_truth () { return true; }

template <bool>
  struct Takes_bool { };

template<bool B>
  using Alias = Takes_bool<B>;

template<typename T>
  struct test { using type = Alias<the_truth<T>()>; };

int main () {
  test<int> a;

  return 0;
}

The compiler is giving me:

main.cpp: In substitution of ‘template<bool B> using Alias = Takes_bool<B>
[with bool B = the_truth<int>()]’:
main.cpp:11:51:   required from ‘struct test<int>’
main.cpp:14:13:   required from here
main.cpp:11:51: error: integral expression ‘the_truth<int>()’ is not constant
   struct test { using type = Alias<the_truth<T>()>; };
                                                   ^
main.cpp:11:51: error:   trying to instantiate ‘template<bool B> using Alias =
Takes_bool<B>’
main.cpp: In function ‘int main()’:
main.cpp:14:13: warning: unused variable ‘a’ [-Wunused-variable]
   test<int> a;
             ^
make: *** [main.o] Error 1

Peeking inside GCC, it seems that the error is thrown from pt.c in
check_instantiated_arg.

Somehow the template constexpr is loosing its "constantness" when expanded in a
dependent context. When the_truth is not a template, there is no error.


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
  2012-12-14 21:36 ` [Bug c++/55663] " mlopez at cse dot tamu.edu
@ 2012-12-14 22:08 ` redi at gcc dot gnu.org
  2012-12-21  9:48 ` paolo.carlini at oracle dot com
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2012-12-14 22:08 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-12-14
     Ever Confirmed|0                           |1

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-12-14 22:06:49 UTC ---
Confirmed, both testcases look valid to me.


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
  2012-12-14 21:36 ` [Bug c++/55663] " mlopez at cse dot tamu.edu
  2012-12-14 22:08 ` redi at gcc dot gnu.org
@ 2012-12-21  9:48 ` paolo.carlini at oracle dot com
  2012-12-22 22:01 ` dodji at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-12-21  9:48 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |leonid at volnitsky dot com

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-12-21 09:47:50 UTC ---
*** Bug 55766 has been marked as a duplicate of this bug. ***


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
                   ` (2 preceding siblings ...)
  2012-12-21  9:48 ` paolo.carlini at oracle dot com
@ 2012-12-22 22:01 ` dodji at gcc dot gnu.org
  2012-12-23 17:18 ` dodji at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dodji at gcc dot gnu.org @ 2012-12-22 22:01 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Dodji Seketeli <dodji at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |dodji at gcc dot gnu.org
         AssignedTo|unassigned at gcc dot       |dodji at gcc dot gnu.org
                   |gnu.org                     |


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
                   ` (3 preceding siblings ...)
  2012-12-22 22:01 ` dodji at gcc dot gnu.org
@ 2012-12-23 17:18 ` dodji at gcc dot gnu.org
  2013-01-15  9:13 ` dodji at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dodji at gcc dot gnu.org @ 2012-12-23 17:18 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

--- Comment #4 from Dodji Seketeli <dodji at gcc dot gnu.org> 2012-12-23 17:18:27 UTC ---
Created attachment 29037
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29037
Candidate patch that I have bootstrapped

This is a candidate patch I'll send a little bit later once the discussion
about my patch for PR c++/52343 at
http://gcc.gnu.org/ml/gcc-patches/2012-12/msg01312.html settles a little bit. 
This is because both patches touch the same area, so there might be some kind
of merging involved at some point.


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
                   ` (4 preceding siblings ...)
  2012-12-23 17:18 ` dodji at gcc dot gnu.org
@ 2013-01-15  9:13 ` dodji at gcc dot gnu.org
  2013-01-15 11:28 ` dodji at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dodji at gcc dot gnu.org @ 2013-01-15  9:13 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

--- Comment #5 from Dodji Seketeli <dodji at gcc dot gnu.org> 2013-01-15 09:12:42 UTC ---
Author: dodji
Date: Tue Jan 15 09:12:30 2013
New Revision: 195189

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195189
Log:
PR c++/55663 - constexpr function templ instantiation

Consider the example of the problem report

     1    template <typename>
     2    constexpr bool the_truth () { return true; }
     3
     4    template <bool>
     5      struct Takes_bool { };
     6
     7    template<bool B>
     8      using Alias = Takes_bool<B>;
     9
    10    template<typename T>
    11      struct test { using type = Alias<the_truth<T>()>; };
    12
    13    int main () {
    14      test<int> a;
    15
    16      return 0;
    17    }

that yields the error:

    test.cc: In substitution of ‘template<bool B> using Alias = Takes_bool<B>
[with bool B = the_truth<int>()]’:
    test.cc:11:51:   required from ‘struct test<int>’
    test.cc:14:13:   required from here
    test.cc:11:51: error: integral expression ‘the_truth<int>()’ is not
constant
       struct test { using type = Alias<the_truth<T>()>; };

I think the issue happens in the course of instantiating test<int> at
line 14, when we look into instantiating Alias<the_truth<T>()> (at
line 11) (using instantiate_alias_template) with T = int.

There, when we check the argument 'the_truth<int>()' to see if it
actually is a constant expression, in check_instantiated_arg, we fail
to recognize it constexpr-ness b/c we just look at its TREE_CONSTANT.

At that point, the_truth<int> should have been folded, and it's not,
because instantiate_alias_template forgets to call
coerce_template_parms on its arguments.

Fixed thus, bootstapped and tested on x86_64-unknown-linux-gnu against
trunk.

gcc/cp/

    PR c++/55663
    * pt.c (coerce_innermost_template_parms): New static function.
    (instantiate_alias_template):  Use it here.

gcc/testsuite/

    PR c++/55663
    * g++.dg/cpp0x/alias-decl-31.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/alias-decl-31.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
                   ` (5 preceding siblings ...)
  2013-01-15  9:13 ` dodji at gcc dot gnu.org
@ 2013-01-15 11:28 ` dodji at gcc dot gnu.org
  2013-01-16 20:23 ` jason at gcc dot gnu.org
  2013-01-21 13:33 ` paolo.carlini at oracle dot com
  8 siblings, 0 replies; 10+ messages in thread
From: dodji at gcc dot gnu.org @ 2013-01-15 11:28 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Dodji Seketeli <dodji at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #6 from Dodji Seketeli <dodji at gcc dot gnu.org> 2013-01-15 11:27:39 UTC ---
Fixed in trunk for 4.8.


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
                   ` (6 preceding siblings ...)
  2013-01-15 11:28 ` dodji at gcc dot gnu.org
@ 2013-01-16 20:23 ` jason at gcc dot gnu.org
  2013-01-21 13:33 ` paolo.carlini at oracle dot com
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2013-01-16 20:23 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |m-ou.se@m-ou.se

--- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> 2013-01-16 20:21:57 UTC ---
*** Bug 55998 has been marked as a duplicate of this bug. ***


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

* [Bug c++/55663] [C++11] Alias template combined with constexpr function is considered non-const
  2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
                   ` (7 preceding siblings ...)
  2013-01-16 20:23 ` jason at gcc dot gnu.org
@ 2013-01-21 13:33 ` paolo.carlini at oracle dot com
  8 siblings, 0 replies; 10+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-01-21 13:33 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55663

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lucdanton at free dot fr

--- Comment #8 from Paolo Carlini <paolo.carlini at oracle dot com> 2013-01-21 13:32:58 UTC ---
*** Bug 56065 has been marked as a duplicate of this bug. ***


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

end of thread, other threads:[~2013-01-21 13:33 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-12 13:37 [Bug c++/55663] New: [C++11] Alias template combined with constexpr function is considered non-const daniel.kruegler at googlemail dot com
2012-12-14 21:36 ` [Bug c++/55663] " mlopez at cse dot tamu.edu
2012-12-14 22:08 ` redi at gcc dot gnu.org
2012-12-21  9:48 ` paolo.carlini at oracle dot com
2012-12-22 22:01 ` dodji at gcc dot gnu.org
2012-12-23 17:18 ` dodji at gcc dot gnu.org
2013-01-15  9:13 ` dodji at gcc dot gnu.org
2013-01-15 11:28 ` dodji at gcc dot gnu.org
2013-01-16 20:23 ` jason at gcc dot gnu.org
2013-01-21 13:33 ` paolo.carlini at oracle dot com

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