Constexpr error in clang but not in gcc?

Take this simple example:

#include <iostream>

namespace foo {
    constexpr int main(int argc, char* argv[]) {
      // code
    }
}

int main(int argc, char* argv[])
{
    return foo::main(argc, argv);
}

Depending on what the code is , clang will complain or not. If the code:

cout << "Hello!";
return 0;

clang complains:

error: constexpr never creates a constant expression [-Winvalid-constexpr]

constexpr int main(int argc, char* argv[]) {

note: the non-conference function operator <<> "cannot be used in constant expression

    std::cout << "Hello!";

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../ include / C ++ / 4.8 / ostream: 530: 5: note: stated here

operator<<(basic_ostream<char, _Traits>& __out, const char* __s)

Fairly enough, constexpr functions cannot contain any cout commands, as we know. But what happens if we do this?

  for (int i = 0; i < argc; i++)
    std::cout << argv[i];

clang allows this! OK, but it cannot be a constexpr function, even if it is marked as constexpr, try using it in the context of constexpr.

int arr[foo::main(argc, argv)];

! , clang? , clang, gcc :

error: constexpr 'constexpr int foo:: main (int, char **) ' return

, - , gcc .

+4
3

++ 1y, constexpr, .

N3652, .

+2

Clang .

:

constexpr int foo::main(int argc, char* argv[]) {
  std::cout << argv[i];
  return 0;
}

++ 11 , , constexpr.

++ 1y , foo::main ( operator<<(std::ostream&, const char*), constexpr).

:

constexpr int foo::main(int argc, char* argv[]) {
  for (int i = 0; i < argc; i++)
    std::cout << argv[i];
  return 0;
}

++ 11 , for -.

++ 1y . , foo::main(0, 0) ( 0). foo::main , Clang .

int arr[foo::main(argc, argv)];

( argc argv, ). , Clang . -pedantic-errors, clang , .

GCC:

error: body of constexpr function 'constexpr int foo:: main (int, char **)' not return-statement

++ 11, ++ 1y. ++ 11 , ( constexpr typedef , return -statements). ++ 1y .

+9

So gcc 4.8.1 does not implement relaxed constexpr constraints, but clang 3.5 does. My mistake was that clang and gcc both have variable array extensions. if I used std :: array, both compilers would reject the code. What I still don't understand is if clang allows relaxed constexpr, then why is it not constexpr?

0
source

Source: https://habr.com/ru/post/1525136/


All Articles