Bug 65977 - Constexpr should be allowed in declaration of friend template specialization
Summary: Constexpr should be allowed in declaration of friend template specialization
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.1.0
: P3 normal
Target Milestone: 6.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-02 07:46 UTC by rhalbersma
Modified: 2015-12-19 08:42 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-06-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rhalbersma 2015-05-02 07:46:24 UTC
The following code comes from this StackOverflow Q&A (note this is not related to std::bitset, it's just a homegrown class to experiment with constexpr): http://stackoverflow.com/q/29871138/819272

#include <cstddef>

template<std::size_t>
class bitset;

template<std::size_t N>
constexpr bool operator==(const bitset<N>&, const bitset<N>&) noexcept;

template<std::size_t N>
class bitset
{
    friend constexpr bool operator== <>(const bitset<N>&, const bitset<N>&) noexcept;
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- error from this piece
};

template<std::size_t N>
constexpr bool operator==(const bitset<N>&, const bitset<N>&) noexcept
{
    return true;
}

int main() {}

The error I get with -std=c++11 on gcc 4.9.1 through gcc HEAD 6.0.0 20150501 is

'constexpr' is not allowed in declaration of friend template specialization 

The same code on older gcc version (4.6 through 4.8) gives a slightly different error

'inline' is not allowed in declaration of friend template specialization

which seems to have been introduced in 2013 https://gcc.gnu.org/ml/gcc-patches/2013-03/msg01028.html

Richard Smith's accepted answer on StackOverflow ( http://stackoverflow.com/a/29957648/819272 ) states that although constexpr functions are implicitly inline, an inline function does not imply the inline specifier. Therefore the above code should be valid.
Comment 1 Paolo Carlini 2015-06-29 19:54:28 UTC
Mine.
Comment 2 paolo@gcc.gnu.org 2015-06-29 22:02:40 UTC
Author: paolo
Date: Mon Jun 29 22:02:08 2015
New Revision: 225148

URL: https://gcc.gnu.org/viewcvs?rev=225148&root=gcc&view=rev
Log:
/cp
2015-06-29  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/65977
	* decl.c (grokfndecl): Allow constexpr declarations of friend
	template specializations.

/testsuite
2015-06-29  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/65977
	* g++.dg/cpp0x/constexpr-friend-3.C: New.
	* g++.dg/cpp0x/constexpr-friend-2.C: Adjust.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-friend-3.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-friend-2.C
Comment 3 Paolo Carlini 2015-06-29 22:03:55 UTC
Fixed.
Comment 4 rhalbersma 2015-12-19 08:42:36 UTC
Ping to get this merged into the upcoming 5.4.