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! 2 1523
Kibiz0r wrote:
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 >
class some_action< T, false {...};
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
On Nov 11, 7:45 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Kibiz0r wrote:
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 >
class some_action< T, false {...};
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: franky.backeljauw |
last post by:
Hello,
I have a problem with using a copy constructor to convert an object of a
templated class to object of another templated class. Let me first
include the code (my question is below):
<code:templates.h>
#include <string>
#include <iostream>
|
by: jack |
last post by:
Hi there,
I have a function F(x, y, z)
and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0)
z0 is fixed.
Suppose somebody wrote a routine called "Compute" which simply
computes f(a) + f(b) for any one dimensional function f.
Then I create a f1dim which converts any n-dimensional function
|
by: Gary |
last post by:
Hi,
I am a Chinese student, I have a problem with the following code
//The follwing code in StaticSearch.h:
//
template <class Type> class dataList;
//
template <class Type> class Node //Êý¾Ý±íÖнáµãÀàµÄ¶¨Òå
|
by: JKop |
last post by:
You know the whole "Linked List" concept, where each object knows where the
the next one is. Well, in one of its simplest forms I have:
template<class T>
struct LinkedObject : virtual public T
{
LinkedObject* p_next;
};
|
by: Alexander Stippler |
last post by:
hi,
the following does not work. I do not understand why, since it works
if I replace line marked by (1) with the line below ((2)). Compiling
with gcc4.0 results in "error: conversion from 'Two<int>' to non-scalar
type 'One<int>' requested". Has somebody an explanation for me?
template <typename T>
struct Convert
| |
by: MurphyII |
last post by:
Just a little sample :
class A
{
public:
A( )
{
}
template<typename T> A( const typename T& a)
{
|
by: Adam Badura |
last post by:
Hi!
I have a class like:
template<int size_integer, int size_fraction>
class number {
template<typename charT, typename traitsT>
explicit number(const charT* textual, int radix = 10);
|
by: Deep |
last post by:
Can I use a class in this manner, where a constructor is of templated:
template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};
|
by: l.s.rockfan |
last post by:
Hello,
how do i have to call an inherited, templated class constructor from the
initializer list of the inheriting, non-templated class constructor?
example code:
template<typename T>
class A
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |