Question

Here is a piece of code:

class Class
{
    static constexpr int getBug();
};

constexpr int Class::getBug()
{
    return 0;
}

What I basically do is declaring a static constepxr method in class declaration, then I implement it.

The original code was split in two file, and contained more methods / attributes which have been stripped, leaving only the required code.

When I compile the code I get the following error from GCC 4.6.0 :

Class.cpp:6:29: internal compiler error: in merge_types, at cp/typeck.c:825
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
  1. Is this really a bug?

  2. In that case, what must I provide my report with?


I've tested the code on an online C++0x compiler and get the following error:

prog.cpp:3:33: error: the 'constexpr' specifier cannot be used in a function declaration that is not a definition
prog.cpp:6:29: error: a constexpr function cannot be defined outside of its class

This compiler uses GCC 4.5.1. It let me know that my code is ill-formed, but introduce more question:

  1. Why does GCC 4.5.1 give an error and GCC 4.6.0 report a bug?

After writing the last paragraph, I tested back on GCC 4.6.0 stripping the static keyword and the separate implementation compiles without any warning!

  1. Why do two compiler of the same family behave so differently?

I know that constexpr methods should avoid any statement different to return, which may explain the GCC 4.5.1 error reporting. As my method uses macro condition to return the good (constant) value, it takes a couple of line which explains why I want to use separated implementation (in addition to the usual recommendation).


My configuration:

Mac OS X.7
GCC 4.6.0
Target: x86_64-apple-darwin10.7.0
Configured with: ./configure
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.7.0' '-v' '-save-temps' '-std=c++0x' '-c' '-shared-libgcc' '-mtune=core2'
Was it helpful?

Solution

This is because constexpr is a new feature of the language, and according to the C++0x Support in GCC page, the support for this feature was first added to GCC in 4.6. My suspicion is that it is in fact a bug in the compiler, that could be fixed in 4.6.1 or later.

GCC 4.5.2 generates an error because the feature isn't yet available in that version. In fact, if you were to check the standard library headers for 4.5.2, you would see that all of the constexpr methods (as dictated by the standard) say something like 'Needs constexpr'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top