473,386 Members | 1,793 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,386 software developers and data experts.

Template Metaprogramming

Hi, I was going through the TMP . I got stuck with the following code . I coundn't understand the declaration of
Expand|Select|Wrap|Line Numbers
  1. Class if_
in the following code ,can anyone help me in figuring out the FLOW OF THE PROGRAM

Expand|Select|Wrap|Line Numbers
  1.  
  2. template <bool Condition, typename TrueResult, typename FalseResult>
  3. class if_;
  4.  
  5. template <typename TrueResult, typename FalseResult>
  6. struct if_<true, TrueResult, FalseResult>
  7. {
  8.   typedef TrueResult result;
  9. };
  10.  
  11. template <typename TrueResult, typename FalseResult>
  12. struct if_<false, TrueResult, FalseResult>
  13. {
  14.   typedef FalseResult result;
  15. };
  16.  
  17. int main()
  18. {
  19.   typename if_<true, int, void*>::result number(3);
  20.   typename if_<false, int, void*>::result pointer(&number);
  21.  
  22.    typedef typename if_<(sizeof(void *) > sizeof(uint32_t)), uint64_t, uint32_t>::result
  23.       integral_ptr_t;
  24.  
  25.    integral_ptr_t converted_pointer = reinterpret_cast<integral_ptr_t>(pointer);
  26. }
  27.  
  28.  
Jul 13 '12 #1

✓ answered by weaknessforcats

This code:
Expand|Select|Wrap|Line Numbers
  1. class if_;
is called a forward reference.

The rule is you cannot use a class name unless the class has already been defined. Th forwrd reference says the class has been defined bu just not in this file o in ts includes.

This assurance by you that class if_ exists somewhere is enough for the compiler to allow and if_* or an if_&. However, if you start executing member functons of if_ you will need the full class definiion.

Usually you see this where the details of if_ need to remain hidden because if_ is a commercial purchase and the seller does not want you to know how if_ is designed. So you are given a header with a forward reference and a library. The library was built using the complete definition soit's OK for you to call a library function using an if_* or an if_& because it is the library code (not you) that calls the member function.

3 24573
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. class if_;
is called a forward reference.

The rule is you cannot use a class name unless the class has already been defined. Th forwrd reference says the class has been defined bu just not in this file o in ts includes.

This assurance by you that class if_ exists somewhere is enough for the compiler to allow and if_* or an if_&. However, if you start executing member functons of if_ you will need the full class definiion.

Usually you see this where the details of if_ need to remain hidden because if_ is a commercial purchase and the seller does not want you to know how if_ is designed. So you are given a header with a forward reference and a library. The library was built using the complete definition soit's OK for you to call a library function using an if_* or an if_& because it is the library code (not you) that calls the member function.
Jul 13 '12 #2
hi ..
when you say IF_* , and IF_& , WHAT DOES IT ESSENTIALLY mean... does it mean if_pointer and if_address(reference)
Jul 18 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
if_ is the name of your class:

Expand|Select|Wrap|Line Numbers
  1. class if_;
  2.  
so if_ is a type like char is a type. Therefore, if_* is a pointer to if_ just like char* is a pointer to char. The & is a reference: if_& means reference to if_ just like char& is a reference to char.

BTW: Sorry for the delay but there have been site problems norifying me of posts needing replies.
Jul 23 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dave | last post by:
For those who might be so inclined, I was wondering if I might get honest critiques of my first real venture into template metaprogramming. This template metaprogram sorts a list of integers at...
12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
21
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it...
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
1
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in...
4
by: Alan Woodland | last post by:
I've been trying out more template metaprogramming ideas with typelists (mostly for personal learning, I'm aware boost most probably provides this facility already), and I've run into this small...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...

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.