473,587 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine constructor from template

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

The problem is that if I put the T(std::istrings tream) 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<typena me T>
T getData() const
{
if (m_data->isSerialized() )
{
const EventData<std:: istringstream>* data =
dynamic_cast<co nst
EventData<std:: istringstream>* >(m_data);
return T(data->getData());
}
else
{
const EventData<T>* data =
dynamic_cast<co nst EventData<T>*>( m_data);
return data->getData();
}
}

Thank you!

Nov 12 '07 #1
2 1530
Kibiz0r wrote:
Basically, I want a templated method to do one thing if the type T has
a constructor that takes a std::istringstr eam, and another thing if it
doesn't.

The problem is that if I put the T(std::istrings tream) 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::istringstr eam convertible to T. I think,
Boost has an

is_convertible< From, To >

template that would assign to

is_convertible< std::istringstr eam, T >::value

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

template < typename T,
bool convertible_fro m_istringstream =
is_convertible< std::istringstr eam, 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<typena me T>
T getData() const
{
if (m_data->isSerialized() )
{
const EventData<std:: istringstream>* data =
dynamic_cast<co nst
EventData<std:: istringstream>* >(m_data);
return T(data->getData());
}
else
{
const EventData<T>* data =
dynamic_cast<co nst EventData<T>*>( m_data);
return data->getData();
}
}

Thank you!

Best

Kai-Uwe Bux
Nov 12 '07 #2
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::istringstr eam, and another thing if it
doesn't.
The problem is that if I put the T(std::istrings tream) 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::istringstr eam convertible to T. I think,
Boost has an

is_convertible< From, To >

template that would assign to

is_convertible< std::istringstr eam, T >::value

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

template < typename T,
bool convertible_fro m_istringstream =
is_convertible< std::istringstr eam, 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<typena me T>
T getData() const
{
if (m_data->isSerialized() )
{
const EventData<std:: istringstream>* data =
dynamic_cast<co nst
EventData<std:: istringstream>* >(m_data);
return T(data->getData());
}
else
{
const EventData<T>* data =
dynamic_cast<co nst 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

Nov 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7143
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>
3
2069
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
2
3065
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 //Êý¾Ý±íÖнáµãÀàµÄ¶¨Òå
3
1391
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; };
15
2571
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
13
2909
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
6
2575
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);
4
1827
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_){} };
4
5544
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
0
7927
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7857
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8352
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7981
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5396
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.