473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Extracting template types from a typedef'd template declaration

Hi everyone,

Yet another syntax problem that's baffling me with templates. I want to
instantiate a template with a single parameter as per normal, however
the parameter is actually a template class itself, with all *its*
parameters filled out (in the form of a typedef.) I can't work out how
to break apart the typedef to reveal what data types were used to create
it in the first place.

Here is some example code that demonstrates the problem. I've tried
every syntax I can think of but I haven't managed to hit upon the right
one yet! Any suggestions would be much appreciated.

Many thanks as always,
Adam.
#include <iostream>

template <class TFrom, class TTo>
class CLinkData;

class A { };
class B { };

typedef CLinkData<A, BMyLink;

// How do you declare the template to accept a CLinkData parameter, but
// broken out into its component types?
//template <template <class TLinkFrom, class TLinkToclass TLinkData>
//template <template <class TLinkFrom, class TLinkTo>
// class TLinkData<TLinkFrom, TLinkTo
template <class CLinkData<TLinkFrom, TLinkTo
void createStoredLink(void)
{
std::cerr << "CLinkData types are " << typeid(TLinkFrom).name() <<
" and " << typeid(TLinkTo).name() << std::endl;
// TLinkFrom should be A, and TLinkTo should be B, the types used
// to declare MyLink.
return;
}

int main(void)
{
createStoredLink<MyLink>();
return 0;
}
Oct 18 '07 #1
3 1827
On Oct 18, 4:40 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
wrote:
Hi everyone,

Yet another syntax problem that's baffling me with templates. I want to
instantiate a template with a single parameter as per normal, however
the parameter is actually a template class itself, with all *its*
parameters filled out (in the form of a typedef.) I can't work out how
to break apart the typedef to reveal what data types were used to create
it in the first place.

Here is some example code that demonstrates the problem. I've tried
every syntax I can think of but I haven't managed to hit upon the right
one yet! Any suggestions would be much appreciated.

Many thanks as always,
Adam.

#include <iostream>

template <class TFrom, class TTo>
class CLinkData;

class A { };
class B { };

typedef CLinkData<A, BMyLink;

// How do you declare the template to accept a CLinkData parameter, but
// broken out into its component types?
//template <template <class TLinkFrom, class TLinkToclass TLinkData>
//template <template <class TLinkFrom, class TLinkTo>
// class TLinkData<TLinkFrom, TLinkTo
template <class CLinkData<TLinkFrom, TLinkTo
void createStoredLink(void)
{
std::cerr << "CLinkData types are " << typeid(TLinkFrom).name() <<
" and " << typeid(TLinkTo).name() << std::endl;
// TLinkFrom should be A, and TLinkTo should be B, the types used
// to declare MyLink.
return;

}

int main(void)
{
createStoredLink<MyLink>();
return 0;

}- Hide quoted text -

- Show quoted text -
Hi,
try one of the following.
Bye,
Francesco B.

#include <iostream>
#include <typeinfo>

template< typename T >
struct CTypeFromType
{};

template <class TFrom, class TTo>
class CLinkData;
class A { };
class B { };
typedef CLinkData<A, BMyLink;

// first method

template< typename TFrom, typename TTo >
void createStoredLinkAux( CTypeFromType< CLinkData< TFrom, TTo )
{
std::cerr << "CLinkData types are " << typeid(TFrom).name() <<
" and " << typeid(TTo).name() << std::endl;

return;

}

template < typename T >
inline void createStoredLink(void)
{
createStoredLinkAux( CTypeFromType< T >() );
}

// second method

template< typename T >
struct CCreateStoredLink
{};

template< typename TFrom, typename TTo >
struct CCreateStoredLink< CLinkData< TFrom, TTo
{
static void Do()
{
std::cerr << "CLinkData types are " << typeid(TFrom).name() <<
" and " << typeid(TTo).name() << std::endl;

return;
}
};

int main(void)
{
createStoredLink<MyLink>();
CCreateStoredLink<MyLink>::Do();
//createStoredLink< int >(); //compile time error
//CCreateStoredLink< int>::Do(); // compile tiem error
std::cin.get();
return 0;
}

Oct 18 '07 #2
Hi,
try one of the following.
Bye,
Francesco B.

// first method
I like this one the best, I'll use that. Thanks for your help! That
CTypeFromType idea is turning out to be quite versatile :-)

Cheers,
Adam.
Oct 19 '07 #3
On Oct 19, 2:34 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
wrote:
Hi,
try one of the following.
Bye,
Francesco B.
// first method

I like this one the best, I'll use that. Thanks for your help! That
CTypeFromType idea is turning out to be quite versatile :-)

Cheers,
Adam.
Have a look at Alexandrescu's "Modern C++ Design" Techniques chapter.
There are quite a few interesting things in there.

Most probably there are many other books like that, but I don't have
much time to read...
;-)
Bye,
FB

Oct 19 '07 #4

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

Similar topics

4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
13
by: Atlas | last post by:
Hi, I implemented a template as: template <int L, int M, int T> class Quantity { ..... public: friend Quantity operator*(const Quantity& q1,const Quantity& q2); ..... };
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
isladogs
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...

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.