public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias
@ 2012-11-13 14:16 niels at penneman dot org
  2012-12-04 21:01 ` [Bug c++/55311] " niels at penneman dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: niels at penneman dot org @ 2012-11-13 14:16 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 55311
           Summary: Cannot specialize template parameter of type 'const
                    char *const' in 'using' alias
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: niels@penneman.org


When providing arguments for a template class in a 'using' alias, specifying
arguments of type 'const char *const' doesn't work. It does work for an 'int'
pointer.

==========

GCC version:

$ gcc -###
Using built-in specs.
COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.0-alpha20121111/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.8.0-alpha20121111/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-4.8.0_alpha20121111/work/gcc-4.8-20121111/configure
--prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.0-alpha20121111
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.0-alpha20121111/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121111
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121111/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121111/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.0-alpha20121111/include/g++-v4
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
--disable-fixed-point --with-ppl --with-cloog --disable-ppl-version-check
--with-cloog-include=/usr/include/cloog-ppl --enable-lto --enable-nls
--without-included-gettext --with-system-zlib --enable-obsolete
--disable-werror --enable-secureplt --enable-multilib
--with-multilib-list=m32,m64 --enable-libmudflap --disable-libssp
--enable-libgomp
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121111/python
--enable-checking=release --disable-libgcj --disable-libquadmath
--enable-languages=c,c++ --enable-shared --enable-threads=posix
--enable-__cxa_atexit --enable-clocale=gnu --enable-targets=all
--with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo
4.8.0_alpha20121111'
Thread model: posix
gcc version 4.8.0-alpha20121111 20121111 (experimental) (Gentoo
4.8.0_alpha20121111)

==========

Code that fails to compile:

==========

template <const char *const C, typename T>
struct A
{};

extern constexpr char HELLO_WORLD[] = "hello world";

template <typename T>
using PartiallySpecialized = A<HELLO_WORLD, T>;

int main(int, char **)
{
        A<HELLO_WORLD, int> original;
        PartiallySpecialized<int> ps;
}

==========

Compiler invocation/output:

$ g++ -std=c++11 -Wall -Wextra strbug.cxx 
strbug.cxx:8:46: error: ‘"hello world"’ is not a valid template argument of
type ‘const char*’ because ‘"hello world"’ is not a variable
 using PartiallySpecialized = A<HELLO_WORLD, T>;
                                              ^
strbug.cxx: In function ‘int main(int, char**)’:
strbug.cxx:12:22: warning: unused variable ‘original’ [-Wunused-variable]
  A<HELLO_WORLD, int> original;
                      ^
strbug.cxx:13:28: warning: unused variable ‘ps’ [-Wunused-variable]
  PartiallySpecialized<int> ps;
                            ^

==========

Notice the first instantiation ('original') does not generate any errors or
warnings. The second instantiation ('ps') should be identical to the second.
Above code compiles without warnings and without errors on Clang 3.1.


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

* [Bug c++/55311] Cannot specialize template parameter of type 'const char *const' in 'using' alias
  2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
@ 2012-12-04 21:01 ` niels at penneman dot org
  2012-12-05  8:33 ` niels at penneman dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: niels at penneman dot org @ 2012-12-04 21:01 UTC (permalink / raw)
  To: gcc-bugs


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

Niels Penneman <niels at penneman dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
      Known to fail|                            |4.6.3, 4.7.2, 4.8.0

--- Comment #1 from Niels Penneman <niels at penneman dot org> 2012-12-04 21:00:50 UTC ---
Exact same thing in a slightly different shape: with a derived struct instead
of a 'using' alias

===================================================

$ g++ -###
Using built-in specs.
COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.0-alpha20121202/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.8.0-alpha20121202/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-4.8.0_alpha20121202/work/gcc-4.8-20121202/configure
--prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.0-alpha20121202
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.0-alpha20121202/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121202
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121202/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121202/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.0-alpha20121202/include/g++-v4
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
--disable-fixed-point --with-ppl --with-cloog --disable-ppl-version-check
--with-cloog-include=/usr/include/cloog-ppl --enable-lto --enable-nls
--without-included-gettext --with-system-zlib --enable-obsolete
--disable-werror --enable-secureplt --enable-multilib
--with-multilib-list=m32,m64 --enable-libmudflap --disable-libssp
--enable-libgomp
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.8.0-alpha20121202/python
--enable-checking=release --disable-libgcj --disable-libquadmath
--enable-languages=c,c++ --enable-shared --enable-threads=posix
--enable-__cxa_atexit --enable-clocale=gnu --enable-targets=all
--with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo
4.8.0_alpha20121202'
Thread model: posix
gcc version 4.8.0-alpha20121202 20121202 (experimental) (Gentoo
4.8.0_alpha20121202) 

===================================================

extern constexpr char STRING_PTR[] = "test";

template <const char *const C, typename T>
struct A {};

template <typename T>
struct B: A<STRING_PTR, T> {};

===================================================

$ g++ -fsyntax-only -std=c++11 test2.cxx 
test2.cxx:7:26: error: ‘"test"’ is not a valid template argument of type ‘const
char*’ because ‘"test"’ is not a variable
 struct B: A<STRING_PTR, T> {};
                          ^

===================================================


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

* [Bug c++/55311] Cannot specialize template parameter of type 'const char *const' in 'using' alias
  2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
  2012-12-04 21:01 ` [Bug c++/55311] " niels at penneman dot org
@ 2012-12-05  8:33 ` niels at penneman dot org
  2012-12-22 16:09 ` dodji at seketeli dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: niels at penneman dot org @ 2012-12-05  8:33 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #2 from Niels Penneman <niels at penneman dot org> 2012-12-05 08:32:42 UTC ---
Workaround for GCC 4.8.0-alpha20121202 (full version details see earlier post)
for case #2: replace STRING_PTR template parameter with static_cast<const char
*const>(STRING_PTR).

Example code below with same invocation arguments is accepted by the compiler.

===================================================

extern constexpr char STRING_PTR[] = "test";

template <const char *const C, typename T>
struct A {};

template <typename T>
struct B: A<static_cast<const char *const>(STRING_PTR), T> {};

===================================================

This workaround does not work for case #1 (with 'using') and does not seem to
work on 4.7.2 in general.


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

* [Bug c++/55311] Cannot specialize template parameter of type 'const char *const' in 'using' alias
  2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
  2012-12-04 21:01 ` [Bug c++/55311] " niels at penneman dot org
  2012-12-05  8:33 ` niels at penneman dot org
@ 2012-12-22 16:09 ` dodji at seketeli dot org
  2012-12-22 16:11 ` dodji at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: dodji at seketeli dot org @ 2012-12-22 16:09 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #3 from dodji at seketeli dot org <dodji at seketeli dot org> 2012-12-22 16:09:18 UTC ---
A candidate patch for this was proposed to
http://gcc.gnu.org/ml/gcc-patches/2012-12/msg01361.html.


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

* [Bug c++/55311] Cannot specialize template parameter of type 'const char *const' in 'using' alias
  2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
                   ` (2 preceding siblings ...)
  2012-12-22 16:09 ` dodji at seketeli dot org
@ 2012-12-22 16:11 ` dodji at gcc dot gnu.org
  2013-01-07  8:07 ` dodji at gcc dot gnu.org
  2013-01-07 11:00 ` dodji at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: dodji at gcc dot gnu.org @ 2012-12-22 16:11 UTC (permalink / raw)
  To: gcc-bugs


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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2012-12-22
         AssignedTo|unassigned at gcc dot       |dodji at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1


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

* [Bug c++/55311] Cannot specialize template parameter of type 'const char *const' in 'using' alias
  2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
                   ` (3 preceding siblings ...)
  2012-12-22 16:11 ` dodji at gcc dot gnu.org
@ 2013-01-07  8:07 ` dodji at gcc dot gnu.org
  2013-01-07 11:00 ` dodji at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: dodji at gcc dot gnu.org @ 2013-01-07  8:07 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #4 from Dodji Seketeli <dodji at gcc dot gnu.org> 2013-01-07 08:06:52 UTC ---
Author: dodji
Date: Mon Jan  7 08:06:46 2013
New Revision: 194961

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=194961
Log:
PR c++/55311 - Cannot specialize alias template with arg of type array of char

Consider this test case:

     1    template <const char *const C, typename T>
     2    struct A
     3    {};
     4
     5    struct B {};
     6
     7    extern constexpr char HELLO_WORLD[] = "hello world";
     8
     9    A<HELLO_WORLD, B> g; // <-- This works fine
    10
    11    template <typename T>
    12    using PartiallySpecialized = A<HELLO_WORLD, T>;  // <-- This fails
    13

At line 12 G++ fails to instantiate the alias template that has a
string variable initialized with a string literal, with the error
message:

    test.cc:12:46: error: ‘"hello world"’ is not a valid template argument of
type ‘const char*’ because ‘"hello world"’ is not a variable
     using PartiallySpecialized = A<HELLO_WORLD, T>;  // <-- This fails
                                              ^

Note that instantiating the template A at line 9 with the same
arguments as in the problematic case above works.

This happens in the context of lookup_template_class_1, when it handles
the alias template instantiation A<HELLO_WORLD, T> and thus passes the
VAR_DECL for HELLO_WORLD to convert_nontype_argument.

Note that from there decay_conversion replaces the the VAR_DECL with
its STRING_CST initializer[1].  Latter on, convert_nontype_argument
checks that the HELLO_WORLD constant it received as argument was
indeed a VAR_DECL:

      else
    {
      tree decl;

      decl = ((TREE_CODE (expr) == ADDR_EXPR)
          ? TREE_OPERAND (expr, 0) : expr);
      if (TREE_CODE (decl) != VAR_DECL)
        {
          error ("%qE is not a valid template argument of type %qT "
             "because %qE is not a variable",
             expr, type, decl);
          return NULL_TREE;
        }

But the issue is, that VAR_DECL has been replaced by STRING_CST, so
the last 'if' above fails.

The fix is to teach decay_conversion to return the address of array,
rather than returning its initializer.

Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.

gcc/cp/

    PR c++/55311
    * pt.c (decay_conversion): Do not return the initializer of an array.

gcc/testsuite/

    PR c++/55311
    * g++.dg/cpp0x/alias-decl-30.C: New test.
    * g++.dg/init/array21.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C
Modified:
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/g++.dg/init/array21.C


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

* [Bug c++/55311] Cannot specialize template parameter of type 'const char *const' in 'using' alias
  2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
                   ` (4 preceding siblings ...)
  2013-01-07  8:07 ` dodji at gcc dot gnu.org
@ 2013-01-07 11:00 ` dodji at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: dodji at gcc dot gnu.org @ 2013-01-07 11:00 UTC (permalink / raw)
  To: gcc-bugs


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

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

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

--- Comment #5 from Dodji Seketeli <dodji at gcc dot gnu.org> 2013-01-07 11:00:32 UTC ---
This should be fixed in trunk (4.8).


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

end of thread, other threads:[~2013-01-07 11:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-13 14:16 [Bug c++/55311] New: Cannot specialize template parameter of type 'const char *const' in 'using' alias niels at penneman dot org
2012-12-04 21:01 ` [Bug c++/55311] " niels at penneman dot org
2012-12-05  8:33 ` niels at penneman dot org
2012-12-22 16:09 ` dodji at seketeli dot org
2012-12-22 16:11 ` dodji at gcc dot gnu.org
2013-01-07  8:07 ` dodji at gcc dot gnu.org
2013-01-07 11:00 ` dodji 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).