2

gcc5.4 doesn't compile the following code:

// source.cpp
int nonconstexprfunc()
{
    return 14;
}

constexpr int func(int n)
{
    if (n < 0)
        return nonconstexprfunc();
    return n*n;
}

int main()
{
    constexpr int t1 = func(0);
    return 0;
}

The command I use:

$ g++ -std=c++14 -c source.cpp

The output:

In function ‘constexpr int func(int)’:
error: ‘constexpr int func(int)’ called in a constant expression
constexpr int t1 = func(0);
In function ‘int main()’:
error: ‘constexpr int func(int)’ called in a constant expression
constexpr int t1 = func(0);

But I can compile that source.cpp using gcc6.4. Doesn't gcc5.4 fully support constexpr functions?

More interestingly I can compile that source.cpp using icpc (Intel C++ compiler) that uses gcc5.4 - I suppose there must be an option to compile that code using gcc5.4.

$  icpc -v
icpc version 19.0 (gcc version 5.4.0 compatibility)
$  icpc -std=c++14 -c source.cpp
no errors
embedc
  • 1,485
  • 1
  • 6
  • 20
  • 1
    icpc doesn't use gcc 5.4, it uses its standard library (header + so/a), and that's it. – Matthieu Brucher Jan 23 '19 at 12:09
  • 1
    Seems to be reproducible on [wandbox](https://wandbox.org/permlink/ZVN4a4COwD3z6Icu). But, you know, no compiler is perfect. GCC 5.4 *could* have had a bug. – StoryTeller - Unslander Monica Jan 23 '19 at 12:12
  • With ICPC do you mean the [International Collegiate Programming Contest](https://en.wikipedia.org/wiki/International_Collegiate_Programming_Contest)? Then why did you add the `icc` tag for the Intel C/C++ Compiler? Please be careful when selecting tags, and edit as soon as you find a mistake. – Some programmer dude Jan 23 '19 at 12:20
  • @Someprogrammerdude icpc is an Intel C++ compiler – embedc Jan 23 '19 at 12:21
  • Okay, so not the competition then. And in that case note that the version messages says "gcc version 5.4.0 ***compatibility***". It's *not* using GCC 5.4, it's only *compatible* with GCC 5.4 (though in this case not that compatible since it didn't implement the GCC bug). The Intel C/C++ Compiler is a totally separate compiler. – Some programmer dude Jan 23 '19 at 12:25

1 Answers1

2

The first limitation is concerning the use gcc 5.4 with -std=c++11 which produces the error because of the two return statement see The body of constexpr function not a return-statement so in order to lift your first issue you need to use -std=c++14

It then produces

'#1 with x86-64 gcc 5.4 : In function 'constexpr int func(int)':

:10:32: error: call to non-constexpr function 'int nonconstexprfunc()'

     return nonconstexprfunc();        ^

: In function 'int main()':

:16:28: error: 'constexpr int func(int)' called in a constant expression

 constexpr int t1 = func(0);

                         Compiler returned: 1

This next error produced seems to be a known GCC bug (misinterpretation of c++14) see
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86678
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67026

You can also check out calling non constexpr function from constexpr allowed in some conditions

However judging from the error it produces:

It seems pretty obvious that doing

constexpr int nonconstexprfunc()
{
    return 14;
}

will solve the error and will be more efficient in your case.
Check the difference with https://www.godbolt.org/ of adding constexpr or not using gcc 8.2 for example.

PilouPili
  • 2,601
  • 2
  • 17
  • 31
  • Thanks! But which version of gcc is being considered in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86678? – embedc Jan 23 '19 at 12:59
  • 1
    The latest 8.2 I believe. – PilouPili Jan 23 '19 at 13:56
  • No) I can compile that source.cpp using gcc6.4 – embedc Jan 24 '19 at 08:24
  • Your example is a little bit different from http:\\gcc.gnu.org/bugzilla/show_bug.cgi?id=86678? . If you try to compile the example constexpr bool always_true() { return true; } int f() { return 1; } constexpr int g() { if (always_true()) return 0; return f(); } you will see that there still is a problem with gcc8.2 and gcc 6.4 for that matter. So I confirm that the gcc being considered is gcc8.2. And to avoid this problem try using constexpr until the gcc 9 release – PilouPili Jan 24 '19 at 08:30
  • Well, if it is a little different then maybe the reason of the error is different too? – embedc Jan 24 '19 at 08:40
  • I can not modify the source code that contains similar original code. But I'd like to know exactly the reason – embedc Jan 24 '19 at 09:27
  • I have edited the answer. If you can't modify the source code I believe your only option is to switch to gcc6.4 with -std=c++14. And beware of the limitation in gcc for the time being. – PilouPili Jan 24 '19 at 12:11