473,396 Members | 2,013 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.

IfThenElse template

I am trying to understand the use of this template and how to use it:
// Primary template.
template<bool C, typename Ta, typename Tb>
class IfThenElse;

// Partial specialization, true.
template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb{
public:
typedef Ta ResultT;
};

// Partial specialization, false.
template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb{
public:
typedef Tb ResultT;
};

int main() {
IfThenElse<true,std::string,intiff;
std::string str = iff.ResultT;
return 0;
}

When instantiated with true it should typedef the second argument
(std::string) to the public fieldResultT. But in main() I get an error
when I try to check the type of ResultT. Why is it not possible to
access ResultT?
May 13 '07 #1
3 2077
On Sun, 13 May 2007 15:45:35 +0200, desktop wrote:
I am trying to understand the use of this template and how to use it:
// Primary template.
template<bool C, typename Ta, typename Tbclass IfThenElse;

// Partial specialization, true.
template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb{
public:
typedef Ta ResultT;
};

// Partial specialization, false.
template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb{
public:
typedef Tb ResultT;
};

int main() {
IfThenElse<true,std::string,intiff; std::string str = iff.ResultT;
return 0;
}

When instantiated with true it should typedef the second argument
(std::string) to the public fieldResultT. But in main() I get an error
when I try to check the type of ResultT. Why is it not possible to
access ResultT?
ResultT is a type so you must use it as a type:

int main() {
IfThenElse<true,std::string,int>::ResultT iff;
std::string str = iff;
return 0;
}

--
Markus Schoder
May 13 '07 #2
Markus Schoder wrote:
On Sun, 13 May 2007 15:45:35 +0200, desktop wrote:
>I am trying to understand the use of this template and how to use it:
// Primary template.
template<bool C, typename Ta, typename Tbclass IfThenElse;

// Partial specialization, true.
template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb{
public:
typedef Ta ResultT;
};

// Partial specialization, false.
template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb{
public:
typedef Tb ResultT;
};

int main() {
IfThenElse<true,std::string,intiff; std::string str = iff.ResultT;
return 0;
}

When instantiated with true it should typedef the second argument
(std::string) to the public fieldResultT. But in main() I get an error
when I try to check the type of ResultT. Why is it not possible to
access ResultT?

ResultT is a type so you must use it as a type:

int main() {
IfThenElse<true,std::string,int>::ResultT iff;
std::string str = iff;
return 0;
}

Ok but why does:

IfThenElse<true,std::string,intbbb;
bbb.ResultT;

or:

IfThenElse<true,std::string,intbbb;
bbb::ResultT;

work, it seems a bit more intuitive.
May 13 '07 #3
On Sun, 13 May 2007 20:14:41 +0200, desktop wrote:
Markus Schoder wrote:
>On Sun, 13 May 2007 15:45:35 +0200, desktop wrote:
>>I am trying to understand the use of this template and how to use it:
// Primary template.
template<bool C, typename Ta, typename Tbclass IfThenElse;

// Partial specialization, true.
template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb{
public:
typedef Ta ResultT;
};

// Partial specialization, false.
template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb{
public:
typedef Tb ResultT;
};

int main() {
IfThenElse<true,std::string,intiff; std::string str = iff.ResultT;
return 0;
}

When instantiated with true it should typedef the second argument
(std::string) to the public fieldResultT. But in main() I get an error
when I try to check the type of ResultT. Why is it not possible to
access ResultT?

ResultT is a type so you must use it as a type:

int main() {
IfThenElse<true,std::string,int>::ResultT iff; std::string str = iff;
return 0;
}


Ok but why does:

IfThenElse<true,std::string,intbbb;
bbb.ResultT;
Drop the typedefs in the template definition above and you get that
behaviour.

--
Markus Schoder
May 13 '07 #4

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

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
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...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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.