how do you call this template constructor? [duplicate]
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.
精彩评论