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 7580017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:48:45+00:00 2026-05-30T17:48:45+00:00

I was halfway through working on this piece of code and thought this is

  • 0

I was halfway through working on this piece of code and thought this is obviously not going to compile before hitting the build button. I was surprised that it not only compiled, but linked and worked as well.

If I were to guess I would say that SFINAE is responsible for it compiling… is it?

struct BaseClass
{
public:
  BaseClass() {}

  template<typename T>
  BaseClass(const T& a_other)
  {
    int i = 0; // for break point
  }

  template<typename T>
  BaseClass& operator= (const T& a_other)
  {
    int i = 0; // for break point
    return *this;
  }

private:

  BaseClass(const BaseClass& a_other); // Does not have a definition
  BaseClass& operator= (const BaseClass& a_other); // Does not have a definition

};

struct MyClass : public BaseClass
{
};

int main()
{
  MyClass i, j;
  i = j;

  return 0;
}

EDIT: I am using Visual-C++ 2008, maybe it is an odd quirk of VS

  • 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-30T17:48:46+00:00Added an answer on May 30, 2026 at 5:48 pm

    The code is not legal.

    i = j calls the implicitly defined copy assignment operator in MyClass. This function calls the copy assignment operator for each of its sub-objects, including direct base classes [class.copy 12.8 p28].

    If you add code to the copy assignment operator for BaseClass you can see where VS is going wrong:

      template<typename T>
      BaseClass& operator= (const T& a_other)
      {
        std::cout << typeid(T).name() << '\n';
        int i = 0; // for break point
        return *this;
      }
    

    For me this prints out “struct MyClass”. VS is calling the BaseClass copy assignment operator by passing the parameter received in MyClass:operator= directly, rather than just the BaseClass sub object of j.

    SFINAE doesn’t come into play because the template functions aren’t failing. VS is simply generating the implicit copy assignment operator incorrectly.

    To sum up:
    VS is generating the implicit copy assignment operator as

    MyClass &operator=(const MyClass& rhs) {
        static_cast<BaseClass&>(*this).operator=(rhs);
        return *this;
    }
    

    When it should be:

    MyClass &operator=(const MyClass& rhs) {
        static_cast<BaseClass&>(*this).operator=(static_cast<const BaseClass&>(rhs));
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this code, About half-way through the concatenation loop, $xml becomes null
More than halfway through the web-service project I am currently working at, my customer
When I compile and run this code (it's part of a much larger program),
I am halfway through an OOP project in the finance industry, and I'm looking
I think i was doing halfway good to get this to halfway work. Anyways
I'm halfway through figuring out a solution to my question, but I have a
I've been going through the screencasts here to learn how to write a table-based
When my Flash website loads, it freezes halfway through the initial animation for 2-3
I'm more than halfway through development of my first iPhone game and I'm currently
Suppose I'm halfway through running an import, and instead of running SELECT COUNT(*) FROM

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.