Determine constructor from template

K

Kibiz0r

Basically, I want a templated method to do one thing if the type T has
a constructor that takes a std::istringstream, and another thing if it
doesn't.

The problem is that if I put the T(std::istringstream) call in there,
every type that is used for that method has to have that constructor
or it errors.

Can someone please point me in the right direction?


Here's (a slightly simplified version of) the code, if you want a look
at it.

template<typename T>
T getData() const
{
if (m_data->isSerialized())
{
const EventData<std::istringstream>* data =
dynamic_cast<const
EventData<std::istringstream>*>(m_data);
return T(data->getData());
}
else
{
const EventData<T>* data =
dynamic_cast<const EventData<T>*>(m_data);
return data->getData();
}
}

Thank you!
 
K

Kai-Uwe Bux

Kibiz0r said:
Basically, I want a templated method to do one thing if the type T has
a constructor that takes a std::istringstream, and another thing if it
doesn't.

The problem is that if I put the T(std::istringstream) call in there,
every type that is used for that method has to have that constructor
or it errors.

Can someone please point me in the right direction?

Such a constructor would make std::istringstream convertible to T. I think,
Boost has an

is_convertible< From, To >

template that would assign to

is_convertible< std::istringstream, T >::value

the value you are interested in. Then, you could use that value for partial
specialization

template < typename T,
bool convertible_from_istringstream =
is_convertible< std::istringstream, T >::value >
class some_action;

template < typename T >
class some_action< T, true > {...};

template < typename T >
Here's (a slightly simplified version of) the code, if you want a look
at it.

template<typename T>
T getData() const
{
if (m_data->isSerialized())
{
const EventData<std::istringstream>* data =
dynamic_cast<const
EventData<std::istringstream>*>(m_data);
return T(data->getData());
}
else
{
const EventData<T>* data =
dynamic_cast<const EventData<T>*>(m_data);
return data->getData();
}
}

Thank you!


Best

Kai-Uwe Bux
 
K

Kibiz0r

Such a constructor would make std::istringstream convertible to T. I think,
Boost has an

is_convertible< From, To >

template that would assign to

is_convertible< std::istringstream, T >::value

the value you are interested in. Then, you could use that value for partial
specialization

template < typename T,
bool convertible_from_istringstream =
is_convertible< std::istringstream, T >::value >
class some_action;

template < typename T >
class some_action< T, true > {...};

template < typename T >
class some_action< T, false > {...};






Best

Kai-Uwe Bux

Awesome. Thanks for the help.

I noticed you can't specify default template parameters for functions.
I got on fine without it, but I'm not entirely confident in my
solution, because I've never done this type of TMP before.

Here's what I came up with: http://pastebin.com/m263b2f9
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top