相关疑难解决方法(0)

什么是内联命名空间?

C++ 11允许inline namespaces,其所有成员也自动在封闭中namespace.我想不出任何有用的应用 - 有人可以给出一个简短,简洁的例子,说明inline namespace需要哪种情况以及最常用的解决方案?

(另外,当发生了什么并不清楚,我namespace声明inline在一个但不是所有的声明,这可能住在不同的文件.这难道不是找麻烦?)

c++ namespaces c++11 inline-namespaces

320
推荐指数
6
解决办法
5万
查看次数

匿名联合和结构

您将如何在标准C++ 11/14中执行此操作?因为如果我没有弄错的话,这不是带有匿名结构的标准兼容代码.

我希望以与此相同的方式访问成员.

template <typename some_type>
struct vec
{
    union {
        struct { some_type x, y, z; };
        struct { some_type r, g, b; };

        some_type elements[3];
    };
};
Run Code Online (Sandbox Code Playgroud)

c++ struct unions c++11

16
推荐指数
3
解决办法
4586
查看次数

在C++中处理C库匿名结构类型

我们有一个庞大的旧C++应用程序,其中包含许多遗留代码和一些用C编写的外部库.这些库很少更新 - 只有当我们发现错误并且供应商提供补丁时.这发生在上周有一个库,在集成新版本后,我们发现如果我们不在本地修改库(我们显然使用上一版本),我们的构建就会出现以下错误消息:

non-local function ‘static E* MyCls::myFct(<anonymous struct>*)’ uses anonymous type
Run Code Online (Sandbox Code Playgroud)

这是因为库声明了许多句柄类型,如下所示:

#define _Opaque struct {unsigned long x;} *

typedef _Opaque    Handle;
typedef _Opaque    Request;
Run Code Online (Sandbox Code Playgroud)

我们在一些类的函数签名中使用它们:

class MyCls {
public:
    static void* myFct(Handle handle);
    ...
}
Run Code Online (Sandbox Code Playgroud)

这会产生上述错误,因为编译器无法为函数创建正确的名称标记名称,因为_Opaque结构没有名称.

我们当前的解决方法是修补库头文件,显式给结构命名:

//#define _Opaque struct {unsigned long x;} * //Replaced by typedef below!
typedef struct __Opaque {unsigned long x;} * _Opaque;
Run Code Online (Sandbox Code Playgroud)

这显然很糟糕,因为如果可能的话我们不想触摸库.另一个更糟糕的选择是将类型转换为void*所有函数签名并将它们转换回各自的类型.并且在纯C中重写每个受影响的函数是最糟糕的选择...

所以,我的问题是:有没有比修补图书馆更好的选择?我有一个简单的解决方案吗?解决这个问题的最佳方法是什么?

c c++ gcc name-mangling anonymous-struct

6
推荐指数
1
解决办法
1654
查看次数

结构变量别名

我正在尝试为结构内的变量创建一个别名,如下所示:

typedef struct {
    union {
        Vector2 position;
        float x, y;
    };
    union {
        Vector2 size;
        float width, height;
    };
} RectangleF;
Run Code Online (Sandbox Code Playgroud)

(请注意,我没有给工会命名所以我不必写:'variable.unionname.x'等)

但是当我创建这个结构的一些常量时,我​​得到一个"初始化器覆盖此子对象的先前初始化"警告:

static const RectangleF RectangleFZero = {
    .x = 0.0f,
    .y = 0.0f, // warning
    .width = 0.0f,
    .height = 0.0f // warning
}
Run Code Online (Sandbox Code Playgroud)

这样做有什么不对吗?如果没有,我怎么能摆脱这个警告?

编辑:我现在使用的解决方案:

typedef struct {
    union {
        Vector2 position;
        struct { float x, y; };
    };
    union {
        Vector2 size;
        struct { float width, height; };
    };
} RectangleF;
Run Code Online (Sandbox Code Playgroud)

c struct c99 compiler-warnings unions

3
推荐指数
1
解决办法
246
查看次数

为什么sizeof给出了错误的答案

好吧,我遇到了一个古怪,也许有人可以解释它.源代码是(c ++ 11):

?#?include? <stdio.h>
struct xyz_ {
    float xyz[3];
    float &x = xyz[0];
    float &y = xyz[1];
    float &z = xyz[2];
};

int main(int argc, char *argv[])
{
    xyz_ xyz;
    xyz.x = 0;
    xyz.y = 1;
    xyz.z = 2;
    xyz.xyz[1] = 1;
    printf("as array %f %f %f\n",xyz.xyz[0],xyz.xyz[1],xyz.xyz[2]);
    printf("as elements %f %f %f\n",xyz.x,xyz.y,xyz.z);
    int sizexyz = sizeof(xyz);
    int sizefloat = sizeof(float);
    printf("float is %d big, but xyz is %d big\n",sizefloat,sizexyz);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

as array 0.000000 1.000000 2.000000
as …
Run Code Online (Sandbox Code Playgroud)

c++11

0
推荐指数
1
解决办法
97
查看次数