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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:28:05+00:00 2026-05-16T08:28:05+00:00

I was pretty sure that the answer to that question was, Never, ever can

  • 0

I was pretty sure that the answer to that question was, “Never, ever can a template be the copy constructor.”

Unfortunately, I just spent 3 hours figuring out why I was getting a warning about recursion, tracked it to the copy constructor, watched the debugger go insane and not let me look at the recursive code, and finally tracked it down to a missing ‘&’ in a base constructor.

You see, I have this complex policy-based design host that’s been working fine for a while now. I went about overriding two policies in one and ran into a recursive copy constructor. Narrowed it down to one policy that is required to provide a constructor that can take a type of XXX concept as its argument, but in this case I’m just discarding it. So I wrote

struct my_policy
{
  template < typename T >
  my_polity(T const) {} // missing '&'...oops
};

Now, my_policy is a base class to the host (of course) and this little typo caused recursion where the host’s copy constructor passed itself up the chain to this, templated constructor rather than an implicit, compiler generated copy constructor. It would then of course call its copy constructor again to create the temporary.

The truly fascinating thing is that I can’t recreate this in simplified code. Even with a sort of mock policy host example I can’t make it happen. The following code does not exhibit the issue:

#include <boost/utility/enable_if.hpp>
#include <boost/mpl/bool.hpp>

struct base
{
  template < typename T >
  base(T const) {}
};

struct another_base 
{
  int x;

  another_base(int y) : x(y) {}
};

template < typename T >
struct is_derived : boost::mpl::false_ {};

template < typename T1, typename T2 >
struct derived : T1, T2
{
  template < typename T >
  derived(T const& x, typename boost::disable_if< is_derived<T> >::type * = 0) : T1(0), T2(x) {}
};

template < typename T1, typename T2 >
struct is_derived<derived<T1,T2>> : boost::mpl::true_ {};

int main() 
{
  derived<base, another_base> d(23);
  derived<base, another_base> x = d;
}

I am using boost’s parameter library to make the 7 or so arguments to the host accessible by “name”. Maybe that’s the issue, I don’t know. At any rate, I’m wondering if someone out there knows what specific conditions, if any, could cause a compiler to legitimately use the templated constructor for “base” as a copy constructor or from the implicit copy constructor for “derived”.

Edit note:

I recreated the problem in the above code by giving “another_base” an explicit copy constructor:

struct another_base 
{
  int x;

  another_base(another_base const& b) : x(b.x) {}

  another_base(int y) : x(y) {}
};

Starting to conclude that this is a compiler bug unless someone can tell me why this is legitimate.

More information:

struct derived;

struct base
{
  base() {}

private:
  base(derived const&);
};

struct base2 
{
  base2() {}
  //base2 (base2 const&) {}
};

struct derived : base, base2 {};

int main()
{
  derived d1; derived d2(d1);
}

Looking more at Schaub’s answer I took the above code and compiled it. It compiles just fine until you uncomment base2’s copy constructor declaration. Then it will blow up in the way I’m assuming was expected with the original code (no access to private constructor in base). So templates aren’t even part of the issue; you can recreate the problem without them. Looks like it’s an MI issue, which VS has always been a little slow at getting right.

I’ve changed the tags to reflect this finding.

Posted to MS’s bug repository

http://connect.microsoft.com/VisualStudio/feedback/details/587787/implicit-copy-constructor-calls-base-with-derived-type-under-specific-conditions

I included a work around in the example code.

  • 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-16T08:28:06+00:00Added an answer on May 16, 2026 at 8:28 am

    I’m pretty sure the answer to your question is “never”.

    Section 12.8.2 of the ANSI C++ Standard says

    A non-template constructor for class X
    is a copy constructor if its first
    parameter is of type X&, const X&,
    volatile X& or const volatile X&, and
    either there are no other parameters
    or else all other parameters have
    default arguments.

    Section 12.8.3 says

    A declaration of a constructor for a
    class X is ill-formed if its first
    parameter is of type (optionally
    cv-qualified) X and either there are
    no other parameters or else all other
    parameters have default arguments. A
    member function template is never
    instantiated to perform the copy of a
    class object to an object of its class
    type. [Example:

    struct S { 
        template <typename T> S(T);
    };
    
    S f();
    
    void g() {
        S a( f() ); // does not instantiate member template
    }
    

    — end example]

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

Sidebar

Related Questions

I'm pretty sure the answer is going to be no here, but I just
I know that it does in PHP, and I'm pretty sure it does in
For anyone that remembers the protocol Avatar, (I'm pretty sure this was it's name)
Pretty sure this question counts as blasphemy to most web 2.0 proponents, but I
I'm pretty sure I know the answer to this, but short of a virtual
I'm pretty sure this is a simple question in regards to formatting but here's
intro: I am pretty sure this is my fault. But I just don't see
I am pretty sure I have seen this before, but I haven't found out
I'm pretty sure most of us are familiar with the concept of a project's
I'm pretty sure stackoverflow.com is created with ASP.NET, but no matter where I click

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.