extern C与C ++避免未定义的行为,在C而不是C + +是合法的? [英] Does extern C with C++ avoid undefined behavior that is legal in C but not C++?

查看:123
本文介绍了extern C与C ++避免未定义的行为,在C而不是C + +是合法的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在C ++文件中使用 extern C ,是否允许在C ++中未定义的C行为?



blah.h

  externC
{
struct x {
int blah;
char buf [];
};

char * get_buf(struct x * base);
struct x * make_struct(int blah,int size);
}

some_random.cpp

  #includeblah.h

...

x * data = make_struct(7,12);
std :: strcpy(get_buf(data),hello);是否在C的灵活数组成员中使用定义的行为,使用这种方式定义行为?

>

解决方案

灵活的数组成员是C的一个标准特性,从1999年的标准开始。它们不存在于C ++中。



您的代码不是有效的C ++。将它包含在 externC中不会更改。合格的C ++编译器必须至少警告它,并且可以应该拒绝它。



实现C风格的灵活数组成员作为C ++的扩展。这是完全合法的(编译器被允许实现扩展),但它的使用是不可移植的。



如果使用 g ++ -pedantic编译它,它的行为就像任何语言扩展一样由编译器定义,而不是由语言定义。 ,您会收到警告:

  c.cpp:5:21:warning:ISO C ++禁止零大小数组'buf'[-Wpedantic] 
char buf [];
^

如果你想在C ++程序中使用C风格的灵活数组成员,依赖于特定于编译器的扩展,您可以将C代码编译为C ,并将其链接到C ++程序中。你不能使用可变的数组成员的类型对你的C ++代码可见,但是你可以在C代码中使用它,也许可以通过一个不透明的指针访问你的C ++代码。有关在同一程序中混用C和C ++的信息,请参见 C ++常见问题。 (或者你可以使用g ++扩展,代价是无法编译你的代码与其他编译器。)



(我假设你使用g ++。一些其他编译器可能实现类似的扩展。)


If you use extern C it with C++ files, does that allow defined C behavior that is undefined in C++?

blah.h

 extern "C"
 {
      struct x {
           int blah;
           char buf[];
      };

      char * get_buf(struct x * base);
      struct x * make_struct(int blah, int size);
 }

some_random.cpp

 #include "blah.h"

 ...

 x * data=make_struct(7, 12);
 std::strcpy(get_buf(data), "hello");

Is using the defined behavior in C's flexible array member, defined behavior when used this way?

解决方案

Flexible array members are a standard feature of C, starting with the 1999 standard. They do not exist in C++.

Your code is not valid C++. Wrapping it in extern "C" doesn't change that. A conforming C++ compiler must at least warn about it, and arguably should reject it.

It happens that g++ implements C-style flexible array members as an extension to C++. That's perfectly legitimate (compilers are allowed to implement extensions), but its use is not portable. Its behavior, like that of any language extension, is defined by the compiler, not by the language.

If you compile it with g++ -pedantic, you'll get a warning:

c.cpp:5:21: warning: ISO C++ forbids zero-size array ‘buf’ [-Wpedantic]
            char buf[];
                     ^

If you want to use C-style flexible array members in a C++ program without relying on a compiler-specific extension, you can compile your C code as C and link it into your C++ program. You can't make the type with the flexible array member visible to your C++ code, but you can use it internally in the C code, and perhaps provide access to it in your C++ code via an opaque pointer. See the C++ FAQ for information about mixing C and C++ within the same program. (Or you can just use the g++ extension, at the cost of not being able to compile your code with other compilers.)

(I'm assuming that you're using g++. Some other compilers probably implement similar extensions.)

这篇关于extern C与C ++避免未定义的行为,在C而不是C + +是合法的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆