MSVC:联合与具有内联友元运算符的类/结构 [英] MSVC: union vs. class/struct with inline friend operators

查看:29
本文介绍了MSVC:联合与具有内联友元运算符的类/结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码在 GCC 3.x 和 4.x 上按预期编译和运行:

This piece of code compiles and runs as expected on GCC 3.x and 4.x:

#include <stdio.h>

typedef union buggedUnion
{   
public:
            // 4 var init constructor
        inline buggedUnion(int _i) {
            i = _i;
        }

        friend inline const buggedUnion operator - (int A, const buggedUnion &B) {
            return buggedUnion(A - B.i);
        }

        friend inline const buggedUnion operator - (const buggedUnion &A, const buggedUnion &B) {
            return buggedUnion(A.i - B.i);
        }

        int i;

} buggedUnion;

int main()
{
    buggedUnion first(10);
    buggedUnion second(5);

    buggedUnion result = 10 - (first - second);

    printf("%d\n", result.i); // 0

    return 0;
}

然而,MSVC 不会编译该代码,抱怨:

MSVC, however, will not compile that code, complaining:

main.cpp(60) : error C3767: '-': candidate function(s) not accessible
        could be the friend function at 'main.cpp(41)' : '-'  [may be found via argument-dependent lookup]
        or the friend function at       'main.cpp(45)' : '-'  [may be found via argument-dependent lookup]
main.cpp(60) : error C2676: binary '-' : 'buggedUnion' does not define this operator or a conversion to a type acceptable to the predefined operator

哪个编译器是正确的?如何解决这个问题?我正在尝试实现干净的代码(没有外部友方方法),同时保持可移植性、灵活性和自文档化代码.

Which of the compilers is correct? How can this be resolved? I'm trying to achieve clean code (no outside friend methods) while maintaining portability, flexibility and self-documenting code.

一些注意事项:

  • 这是一个显示问题的测试用例,原始数据类型更加复杂和精心设计,尽管在 MSVC 中不起作用(主编译器是 GCC,但 MSVC 兼容性也是需要).
  • 在联合声明的开头添加public:"并不能解决问题.
  • 在每个运算符未解析之前添加public:"
  • 将测试用例转换为结构/类确实修复了它,但这不是我们想要的(请不要发火,我有理由.其中大多数是 C++ 语言的限制)
  • 运算符方法应保留在全局范围(不是成员函数)
  • This is a test-case to show the problem, the original data-type is much more sophisticated and carefully designed, albeit not working in MSVC (main compiler is GCC, though MSVC compatibility is also desired).
  • Adding 'public:' at the start of the union declaration does not resolve it.
  • Adding 'public:' before each operator does not resolve it
  • Converting the test case to a struct/class does fix it, but this is not desired (Please no flames, I got reasons. Most of them are limitations of the C++ language)
  • Operator method is to be left at global scope (not a member function)

出于美学原因(超过 24 种不同的运算符和操作数组合),最佳解决方案不会依赖于将声明移到联合定义之外,但如果没有其他解决方案,则会这样做.

Optimal solution would not rely on moving the declaration outside of the union definition for aestetic reasons (over 24 different combinations of operators and operands), but will be done if there is no other solution.

推荐答案

很难说哪个是正确的,因为标准不允许未命名的 struct(尽管它们是一个常见的扩展名),因此程序格式错误.

It is difficult to say which one is right, since unnamed structs are not allowed by the standard (although they are a common extension), and as such the program is ill-formed.

编辑:这似乎是 msvc 中的一个错误,因为以下完全有效的代码无法编译.

Edit: It does seem to be a bug in msvc, since the following code, which is perfectly valid, fails to compile.

union buggedUnion
{
    friend buggedUnion operator - (int A, const buggedUnion &B) {
        return B;
    }

    friend buggedUnion operator - (const buggedUnion &A, const buggedUnion &B) {
        return A;
    }

    int i;
};


int main()
{
    buggedUnion first = { 1 };
    buggedUnion second = { 1 };
    buggedUnion result = 3 - (first - second);
}

您可以通过在类外定义函数来解决此问题.

You can work around this by defining the functions outside the class.

union buggedUnion
{
    int i;
};

buggedUnion operator - (int A, const buggedUnion &B) {
    return B;
}

buggedUnion operator - (const buggedUnion &A, const buggedUnion &B) {
    return A;
}

您甚至可以通过在类内部声明函数(但仍然在外部定义它们)来保留友元状态,但我怀疑您在联合中是否需要这样做.

You can even retain the friend status by declaring the functions inside the class (but still defining them outside), but I doubt you'd ever need that in a union.

请注意,我删除了不必要的 typedefinlines.

Note that I removed the unnecessary typedef and inlines.

这篇关于MSVC:联合与具有内联友元运算符的类/结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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