tags:

views:

113

answers:

2

Possible Duplicate:
C++ invoke explicit template constructor

Hi,

template <typename T>
class testing
{
public:

    template <typename R>
    testing()
    {
       // constructor A
    }

    template <typename R>
    testing(const R&)
    {
       // constructor B
    }
};

What is the syntax to invoke the constructor A?

I will need the type to be passed during the constructor call. Is there a way to call it? constructor B is a workaround as I only need to know the type not the whole object.

Thanks,

Stephen

+1  A: 

You can't. The class template is based on the type T, so any template parameter you pass while instantiating the class will match T and not R.

Edit: You may also find this post useful: Constructor templates and explicit instantiation

You should go ahead with the workaround (constructor B). Most modern compilers will optimize out the unused parameter, so it should make no difference.

casablanca
Yes. the constructor A (constructor without argument) cannot be called. Although the compiler does not seem to give warning when it sees the class definition, there is just no way to call it.
rwong
+3  A: 

you can create workaround:

template<class A>
testing(boost::mpl::identity<A>);

// and instantiate like this
testing(boost::mpl::identity<A>());

I asked very similar question before http://stackoverflow.com/questions/2786946/c-invoke-explicit-template-constructor

aaa