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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:26:47+00:00 2026-05-27T14:26:47+00:00

Here is a minimal code that shows the problem: template<typename To, typename From> To

  • 0

Here is a minimal code that shows the problem:

template<typename To, typename From> To convert(const From& x);

struct A
{
    int value;
    template<typename T> A(const T& x) { value = convert<A>(x).value; }
};

struct B : public A { };

int main()
{
    B b;
    A a = b;
}

It gives me: undefined reference to 'A convert<A, B>(B const&)'

As expected, as I removed the default copy constructor. But if I add this line to A:

A(const A& x) { value = x.value; }

I get the same error. If I try to do this way: (adding a template specialization)

template<> A(const A& x) { value = x.value; }

I get: error: explicit specialization in non-namespace scope 'struct A'.

How to solve it?

My compiler is MinGW (GCC) 4.6.1

EDIT:

The convert functions converts from many types to A and back again. The problem is that don’t make sense writing a convert function from B to A because of the inheritance. If I remove the line that calls convert from A it just works. The idea is to call convert for all times that do’t inherit from A, for these, the default constructor should be enough.

  • 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-27T14:26:48+00:00Added an answer on May 27, 2026 at 2:26 pm

    As for as I understand, when b is passed, as b is not an object of A, copy constructor is not called, instead the template constructor is called.

    However, if an object of the derived class is passed, you want the copy constructor of A to be called.

    For this, there is one solution using <type_traits> (c++0x):

    #include <type_traits>
    
    template<typename To, typename From> To convert(const From& x);
    
    struct A
    {
        int value;
        template<typename T> A(const T& x, 
            const typename std::enable_if<!std::is_base_of<A,T>::value, bool>::type = false) 
        { value = convert<A>(x).value; }
        A(){}
    };
    
    struct B : public A { };
    
    int main()
    {
        B b;
        A a = b;
    }
    

    The template is disabled why an object of a class derived from A is passed, so the only available constructor is the copy constructor.

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

Sidebar

Related Questions

Here's an interesting problem to solve in minimal amounts of code. I expect the
Here's a minimal version of the code that took me a lot of time
Here's a minimal code that puzzles me : #include <list> class A; class A
Here's a minimal batch file, demo.bat , to illustrate my problem: @ECHO off set
Sample: I've created a minimal set of files that highlight the issue here: http://uploads.omega.org.uk/Foo3.zip
I came across some code which kind of puzzled me. Here's a minimal example
Here is the code: https://www.dropbox.com/s/o42wy36x4qhrbpt/PDFScroller.zip I took the WWDC 2010 PhotoScroller sample code that
First here's what I'm using and trying to do: the minimal setup for this
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

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.