[Bug c++/78022] New: Strange C++ compilation error

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] New: Strange C++ compilation error

law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

            Bug ID: 78022
           Summary: Strange C++ compilation error
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fiesh at zefix dot tv
  Target Milestone: ---

The following piece of code does not compile.  Since clang 3.8.1 compiles it,
and furthermore changes that would appear inconsequential make it compile, I
suspect it might be an issue of GCC:

------------------
#include <array>

template<typename T>
struct A
{
        template<typename... Args>
                constexpr A(Args&&... args) :
                        entries_{std::forward<Args>(args)...} {}
        std::array<T, 1> entries_;
};

struct B : public A<int>
{
        using A<int>::A;
};

// Uncommenting the following line makes this file compile.
//constexpr B p{0};
// Commenting the following line makes this file compile.
template<typename>
struct X
{
        X()
        {
                // The following line is fine.
                const B b0{0};
                // The following line produces an error.
                constexpr B b1{0};
        }
};
------------------

The output is as follows:
% g++ -std=c++11 -c test.cpp
test.cpp: In constructor ‘X<T>::X()’:
test.cpp:31:20: error: ‘constexpr B::B(Args&& ...) [with Args = {int}]’ used
before its definition
    constexpr B b1{0};
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] Strange C++ compilation error

law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

--- Comment #1 from fiesh at zefix dot tv ---
The same happens with 6.2.1.
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] constexpr int template rejected unless constructor is used before hand

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2016-10-19
            Summary|Strange C++ compilation     |constexpr int template
                   |error                       |rejected unless constructor
                   |                            |is used before hand
     Ever confirmed|0                           |1
      Known to fail|                            |5.3.0, 5.4.0, 7.0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
struct X can be a function too.
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] constexpr int template rejected unless constructor is used before hand

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

--- Comment #3 from fiesh at zefix dot tv ---
Interestingly, clang-5 no longer compiles this code:

<source>:28:15: error: constexpr variable 'b1' must be initialized by a
constant expression
                constexpr B b1{0};
                            ^~~~~
<source>:28:15: note: constructor inherited from base class 'A<int>' cannot be
used in a constant expression; derived class cannot be implicitly initialized
<source>:14:16: note: declared here
        using A<int>::A;
                      ^
1 error generated.
Compiler returned: 1

So gcc, clang <=4 and clang-5 behave pairwise differently.  At most one of them
can be right, who might it be?
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] constexpr int template rejected unless constructor is used before hand

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2016-10-19 00:00:00         |2018-2-12
      Known to fail|                            |8.0

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Still fails on trunk (and Clang-5+ as reported). Clang-4 and EDG 4.14 accept
it.

Slightly reduced:

template<typename T>
struct A
{
        template<typename... Args>
                constexpr A(Args&&...){}
};

struct B : public A<int>
{
        using A<int>::A;
};

// Uncommenting the following line makes this file compile.
//constexpr B p{0};
// Commenting the following line makes this file compile.
template<typename>
struct X
{
        X()
        {
                // The following line is fine.
                const B b0{0};
                // The following line produces an error.
                constexpr B b1{0};
        }
};
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] constexpr int template rejected unless constructor is used before hand

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

--- Comment #5 from fiesh at zefix dot tv ---
The code still does not compile with gcc-8.2 but does compile on trunk now!

Conversely, clang consistently refuses to compile it since clang-5.

So I guess this can be closed, unless clang is actually correct and my bug
report was flawed because the code is actually invalid?

For what it's worth, godbolt shows that both MSVC and ICC accept the code as
valid.
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] constexpr int template rejected unless constructor is used before hand

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
That was fixed by r261084.
Reply | Threaded
Open this post in threaded view
|

[Bug c++/78022] constexpr int template rejected unless constructor is used before hand

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78022

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Or, let's say, started to be accepted.