Bug 60245 - function with using defined parameter not accepted as constexpr parameter
Summary: function with using defined parameter not accepted as constexpr parameter
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2014-02-17 16:46 UTC by Florent Hivert
Modified: 2014-11-18 11:58 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
preprocessed code (51.14 KB, text/plain)
2014-02-17 16:47 UTC, Florent Hivert
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Florent Hivert 2014-02-17 16:46:51 UTC
GCC is not able to compile the following code

#include <array>

using Ar = std::array<unsigned long, 10>;

template<typename T>
constexpr T Apply(const T& in, T (*f)(const T&)) { return f(in); }

static constexpr Ar id(const Ar& line) { return line; }
static constexpr Ar ar1 = {{1}};
static constexpr Ar results1 = Apply<Ar>(ar1, &id);

It complains that 

bug4.cpp:10:50:   in constexpr expansion of ‘Apply<std::array<long unsigned int, 10ul> >((* & ar1), id)’
bug4.cpp:6:63: error: expression ‘id’ does not designate a constexpr function
 constexpr T Apply(const T& in, T (*f)(const T&)) { return f(in); }

This seems to be a problem with template substitution since manually specializing the template makes the error vanish:

#include <array>

using Ar = std::array<unsigned long, 10>;

template<typename T>
constexpr T Apply(const T& in, T (*f)(const T&)) { return f(in); }

// manual specialization:
template<>
constexpr Ar Apply<Ar>(const Ar& in, Ar (*f)(const Ar&)) { return f(in); }

static constexpr Ar id(const Ar& line) { return line; }
static constexpr Ar ar1 = {{1}};
static constexpr Ar results1 = Apply<Ar>(ar1, &id);


System informations:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/4.8/lto-wrapper
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.8 --enable-ssp --disable-libssp --disable-plugin --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --enable-linker-build-id --program-suffix=-4.8 --enable-linux-futex --without-system-libunwind --with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.8.1 20130909 [gcc-4_8-branch revision 202388] (SUSE Linux)
Comment 1 Florent Hivert 2014-02-17 16:47:40 UTC
Created attachment 32155 [details]
preprocessed code
Comment 2 Florent Hivert 2014-02-17 17:10:08 UTC
Sorry ! The version I submitted is not the most reduced. Here is a version not using vectors:

constexpr int Apply(const int in, int (*f)(const int&)) { return f(in); }

using Foo = int;
static constexpr int id(const Foo& i) { return i; }
static constexpr int results1 = Apply(0, &id);


Note: Replacing the Foo by int in the definition of id makes the problem vanish.
Comment 3 Florent Hivert 2014-05-28 10:02:08 UTC
The problem remains with the newly released gcc 4.9.0. I upgraded the version tag.
Comment 4 Paolo Carlini 2014-11-18 11:46:27 UTC
This is fixed in mainline, I'm adding the testcase and closing the bug.
Comment 5 paolo@gcc.gnu.org 2014-11-18 11:57:47 UTC
Author: paolo
Date: Tue Nov 18 11:57:16 2014
New Revision: 217709

URL: https://gcc.gnu.org/viewcvs?rev=217709&root=gcc&view=rev
Log:
2014-11-18  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60245
	* g++.dg/cpp0x/constexpr-60245.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-60245.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 6 Paolo Carlini 2014-11-18 11:58:16 UTC
Done.