1

The following code compiles with gcc but does not compile with MSVC (Godbolt link)

#include <array>

extern "C" std::array<float,3> foo()
{
    return std::array<float,3>{1,2,3};
}

Is one of the compilers is right and the other is wrong, or, is it implementation-defined ?

Anonymous
  • 27
  • 2
  • Can you run the code compiled with gcc? – StureS Jul 28 '21 at 10:59
  • @StureS It's right there `The following code compiles with gcc` – KamilCuk Jul 28 '21 at 11:01
  • Yes, compiles! But can you **run** it? – StureS Jul 28 '21 at 11:03
  • @StureS Sure - [godbolt link](https://godbolt.org/z/fPs87jj6f) - runs in gcc. – KamilCuk Jul 28 '21 at 11:06
  • clang compiles it but issues a warning "may be incompatible with C linkage". What does the standard say for a C function returning a C++ type? [This combination does not seem to be mentioned.](https://eel.is/c++draft/dcl.link) I'd say undefined? Actually according to `10` it may be implementation-defined. –  Jul 28 '21 at 11:07
  • The memory representation of `std::array` is just `float[3]` which is also a valid type in C, so it makes at least some sense that it compiles. – Henri Menke Jul 28 '21 at 11:07

0 Answers0