Bug 55931 - [C++11] Constexpr member function inside a static member is not working
Summary: [C++11] Constexpr member function inside a static member is not working
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.2
: P3 normal
Target Milestone: 4.9.0
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2013-01-10 05:04 UTC by Vincent Reverdy
Modified: 2013-12-09 22:57 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-01-11 00:00:00


Attachments
55931.patch (714 bytes, patch)
2013-02-15 19:29 UTC, Jason Merrill
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Reverdy 2013-01-10 05:04:20 UTC
The call of a constexpr member of a class into a static member of the same class is not working. This is illustrated by the following code:
(tested on g++ 4.7.1 and 4.7.2, but it may also affect g++ 4.8)

-------------------------------------------------------------------
#include <iostream>
#include <type_traits>

template<typename Type>
class Test
{
    public:
        constexpr Test(const Type val) : _value(val) {}
        constexpr Type get() const {return _value;}
        static void test()
        {
            static constexpr Test<int> x(42);
            std::integral_constant<int, x.get()> i; // This is not working
            std::cout<<i<<std::endl;
        }
    protected:
        Type _value;
};

int main(int argc, char *argv[])
{
    static constexpr Test<int> x(42);
    std::integral_constant<int, x.get()> i; // This is working
    std::cout<<i<<std::endl;
    Test<double>::test();
    return 0;
}
-------------------------------------------------------------------

which produces the following error:

-------------------------------------------------------------------
main.cpp: In static member function ‘static void Test<Type>::test()’:
main.cpp:13:48: erreur: invalid use of ‘Test<Type>::get<int>’ to form a pointer-to-member-function
main.cpp:13:48: note:   a qualified-id is required
main.cpp:13:48: erreur: could not convert template argument ‘x.Test<Type>::get<int>()’ to ‘int’
main.cpp:13:51: erreur: invalid type in declaration before ‘;’ token
-------------------------------------------------------------------
Comment 1 Andrew Pinski 2013-01-10 05:18:11 UTC
>             std::integral_constant<int, x.get()> i; // This is not working

Most likely because Test<int> is not complete at the time of parsing.
Comment 2 Daniel Krügler 2013-01-11 13:22:13 UTC
When using gcc 4.8.0 20130106 (experimental)

with flags

-Wall -std=c++11 -pedantic

I get an internal compiler error for the line:

static constexpr Test<int> x(42);

"12|internal compiler error: tree check: expected target_expr, have cast_expr in check_initializer, at cp/decl.c:5699"

Should this be reported separately?
Comment 3 Daniel Krügler 2013-01-11 13:26:46 UTC
(In reply to comment #1)
> Most likely because Test<int> is not complete at the time of parsing.

But this should be considered as a compiler defect, right?
Comment 4 Jonathan Wakely 2013-01-11 14:37:36 UTC
(In reply to comment #2)
> Should this be reported separately?

Yes please, but note this PR in the comments and vice versa, in case they turn out to be fixed by the same change.

(In reply to comment #3)
> But this should be considered as a compiler defect, right?

Right.
Comment 5 Daniel Krügler 2013-01-11 18:32:13 UTC
The ICE bug of 4.8.0 has been submitted as bug 55944
Comment 6 Jason Merrill 2013-02-15 19:29:29 UTC
Created attachment 29473 [details]
55931.patch

Patch waiting for 4.8 to branch.
Comment 7 Jason Merrill 2013-03-17 02:40:06 UTC
Author: jason
Date: Sun Mar 17 02:39:51 2013
New Revision: 196746

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196746
Log:
	PR c++/55931
	* parser.c (cp_parser_template_argument): Don't
	fold_non_dependent_expr.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-template4.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
Comment 8 Jason Merrill 2013-04-01 21:05:51 UTC
Fixed for 4.9.
Comment 9 Ryan Mansfield 2013-12-09 22:53:52 UTC
(In reply to Jason Merrill from comment #6)
> Created attachment 29473 [details]
> 55931.patch
> 
> Patch waiting for 4.8 to branch.

Jason, do you mind moving this over to the 4.8 branch? It seems you intended to.