开发者

how do you call this template constructor? [duplicate]

This question already has an answer here: Closed 12 years ago.

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
   开发者_StackOverflow社区 }
};

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


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 C++ invoke explicit template constructor


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜