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

  • Home
  • SEARCH
  • 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 7576727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:59:30+00:00 2026-05-30T16:59:30+00:00

I’m using a template class with CRTP to implement the clone pattern, with a

  • 0

I’m using a template class with CRTP to implement the clone pattern, with a second template parameter Base to allow for multiple levels of inheritance. I get a compiler error when I try to invoke the indirect base class’s constructor.

class B
{
public:
    B() {} //trivial constructor
    virtual B* clone()=0;
};

template<class Base, class Derived>
class Clonable
    :public Base //weird, I know
{
public:
    virtual B* clone() {return new Derived(*this);}
};

class D1 : public Clonable<B, D1>
{
public:
    D1(int a); //non-trivial constructor. Different signature than B
};

class D2 : public Clonable<D1, D2>
{
public:
    D2(int a): D1(a) {} //compiler error here
}

The only solution I’ve come across so far is to use a variadic template constructor in Cloneable, but my compiler (VC++11) hasn’t implemented them yet.

  • 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-30T16:59:31+00:00Added an answer on May 30, 2026 at 4:59 pm

    You need to let your cloning “middleman” class forward constructor arguments, or better (Luc Danton suggested this) use C++11 constructor inheritance.

    So, it’s easy to do this in C++11, but it’s not so easy in C++03 or with a current compiler that doesn’t yet support C++11 argument forwarding or constructor inheritance, such as Visual C++10.

    One way to do that in C++03, using a helper argument forwarder class, is discussed in my old blog posting “3 ways to mix in a generic cloning implementation”. Then the middleman (cloning implementation) class can look like this:

    template< class Derived, class Base >
    class WithCloningOf
        : public progrock::cppx::ConstructorArgForwarder< Base >
    {
    protected:
        virtual WithCloningOf* virtualClone() const
        {
            return new Derived( *static_cast< Derived const* >( this ) );
        }
    
    public:
        template< class ArgPack >
        WithCloningOf( ArgPack const& args )
            : progrock::cppx::ConstructorArgForwarder< Base >( args )
        {}
    
        std::auto_ptr< Derived > clone() const
        {
            return std::auto_ptr< Derived >(
                static_cast< Derived* >( virtualClone() )
                );
        }
    };
    

    I discussed the C++03 compatible ConstructorArgForwarder in earlier blog posting; it can look like this:

    template< typename Type >
    class ConstructorArgForwarder
        : public Type
    {
    public:
        typedef Type        Base;
    
        // TODO: remove
        virtual ~ConstructorArgForwarder() {}
    
        ConstructorArgForwarder( EmptyArgPack const& )
            : Base()
        {}
    
        template< class T01 >
        ConstructorArgForwarder(
            ArgPack< T01 > const& args
            )
            : Base( args.a01 )
        {}
    
        template< class T01, class T02 >
        ConstructorArgForwarder(
            ArgPack< T01, T02 > const& args
            )
            : Base( args.a01, args.a02 )
        {}
    
        template< class T01, class T02, class T03 >
        ConstructorArgForwarder(
            ArgPack< T01, T02, T03 > const& args
            )
            : Base( args.a01, args.a02, args.a03 )
        {}
    
        // And more, up to max 12 arguments.
    };
    

    It in turn uses an argument pack class ArgPack (well OK, class template), which can look like this:

    enum NoArg {};
    
    template<
        class T01 = NoArg, class T02 = NoArg, class T03 = NoArg,
        class T04 = NoArg, class T05 = NoArg, class T06 = NoArg,
        class T07 = NoArg, class T08 = NoArg, class T09 = NoArg,
        class T10 = NoArg, class T11 = NoArg, class T12 = NoArg
        >
    struct ArgPack;
    
    template<
        >
    struct ArgPack<
        NoArg, NoArg, NoArg, NoArg, NoArg, NoArg,
        NoArg, NoArg, NoArg, NoArg, NoArg, NoArg
        >
    {};
    
    typedef ArgPack<
        NoArg, NoArg, NoArg, NoArg, NoArg, NoArg,
        NoArg, NoArg, NoArg, NoArg, NoArg, NoArg
        >                                           EmptyArgPack;
    
    inline ArgPack<> args() { return ArgPack<>(); }
    
    template<
        class T01
        >
    struct ArgPack<
        T01, NoArg, NoArg, NoArg, NoArg, NoArg,
        NoArg, NoArg, NoArg, NoArg, NoArg, NoArg
        >
    {
        T01 const&  a01;
        ArgPack( T01 const& v01 )
            : a01( v01 )
        {}
    };
    
    template< class T01 >
    inline ArgPack< T01 >
    args( T01 const& a01 )
    {
        return ArgPack< T01 >( a01 );
    }
    

    Disclaimer: erors may just have sneaked in e.g. in copying the code from my blog. However, it worked at the time I posted about it, in May 2010.

    Note: As I discuss at in the last of the two above blog postings, about cloning, there three main general ways to do it, and of these the simple macro beats the other two with good margin, for C++03. However, with C++11 the “middleman” approach you’ve chosen here seems better. The “sideways inheritance” via dominance is just complicated and inefficient, but if you are restricted to C++03, then do consider a simple macro!

    Note 2: The last time I suggested doing the practical & sensible thing, I was heavily downvoted (presumably by Reddit kids). Since then, however, I have stopped caring about SO rep points, and in particular downvotes. So, happily, I can now again give good advice, just like in the old Usenet days, just ignoring them downvoter kids’ mindless reaction to certain words. 🙂

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.