Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 274845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:40:21+00:00 2026-05-12T00:40:21+00:00

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

  • 0

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, 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.

Some notes:

  • 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)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:40:22+00:00Added an answer on May 12, 2026 at 12:40 am

    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.

    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.

    Note that I removed the unnecessary typedef and inlines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code which compiles and works as expected: #include <iostream>
I am using std::unique_ptr in this piece of code which compiles and runs as
Why this piece of code compiles? #include <iostream> int foo(int x) { if(x ==
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
This piece of code compiles OK in VS 2010 in a framework 3.5 project
If I run this piece of code through closure compiler or uglifyjs, this.init is
This piece of code compiles file in VC6 but in VS 2008 it gives
I have this simple piece of code that uses boost::bind: #include <boost/bind.hpp> #include <utility>
I have this very simple piece of code; #include <deque> #include <vector> using namespace
This piece of code is from Adobe docs : package { import flash.display.Loader; import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.