Topic: N1618 - Template constructors of template classes


Author: jgottman@carolina.rr.com ("Joe Gottman")
Date: Tue, 27 Apr 2004 00:57:41 +0000 (UTC)
Raw View
Proposal N1618 in the Post-Sydney mailing proposes a way for one constructor
to delegate to another.  For instance:

class X {
    int i_;
    public:
    X( int i ) : i_(i) { }
    X() : X(42) { } // i_ == 42
};

   I think this is a very good idea, but the suggested implementation runs
into a bit of a snag when a template class tries to forward to a template
constructor.  The proposal gives the following example of forwarding to a
template constructor:

class X {
    template<class T> X( T, T ) : l_( first, last ) { /*Common Init*/ }
    list<int> l_;
public:
    X( vector<short>& );
    X( deque<char>& );
};
X::X( vector<short>& v ) : X( v.begin(), v.end() ) { }
// T is deduced as vector<short>::iterator
X::X( const deque<char>& d ) : X<deque<char>::iterator>( d.begin(),
d.end() ) { }
// T does not need to be deduced

But what if the class X above were a template class?  In that case, would
the line

    X::X( const deque<char>& d ) : X<deque<char>::iterator>( d.begin(),
d.end() ) { }

attempt to call a template constructor, or would it try to instantiate a
non-existent base class of type X<deque<char>::iterator>?

Joe Gottman



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]