Boost logo

Boost :

From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2007-11-19 16:58:31

On Monday 19 November 2007 22:11:37 Sid Sacek wrote:
> template<class T> class shared_ptr
> {
> template<class Y>
> explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
> {
> detail::sp_enable_shared_from_this( pn, p, p );
> }
> };
> I've tried really hard to figure out what it means when a constructor is
> both a template as well as explicit. The keywords have incompatible
> meanings to me and therefore I fail to make sense of it. If you template
> the argument, then you're saying you'll accept any type of pointer as long
> as it will still compile, and the explicit implies no explicit conversions
> to Y, which shouldn't happen anyway due to the template indicating any
> argument is valid as long as it complies.

To be honest, I don't know, but did you try to remove the explicit? You don't
have to recreate/copy the whole shared_ptr class to see it:

struct test
   test(test const&);
   template<typename T>
   explicit test(T* p);

void function(test t);

int main() {
   void* p;

I'd say this is equivalent, only that test is not a class template. Now, if
you remove the 'explicit', it suddenly compiles.

> I googled for this and researched the C++ Templates book without success.
> Also, nobody I've asked this about could give me a satisfactory answer, and
> so I'm hoping somebody on the boost list won't mind enlightening me by
> providing a few concrete examples where it comes into play.

I'd say this is a general C++ question and thus not really the goal of the
mailinglist. I'd suggest you ask the question in the Usenet, in
comp.lang.c++.moderated to be precise. Typically you get very good answers


Boost list run by bdawes at, gregod at, cpdaniel at, john at