473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(very) Simple Template Metaprogramming question

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 Gurtovoy. I’m
not doing this because I want to be a Meta Programming guru (because a
lot of that stuff looks too crazy for use in the real world).

Rather I want to learn heavyweight templates and this is the only
hardcode template book that has questions and answers (on the web).

Anyway I have got as far as the first exercise. I have done the
exercise but I’m getting a compile error below.

I’m using Visual Studio 2005, but I’m sure its not a compiler problem
– its just I don’t understand what is going on.

The compile error is on the line (full code below):

const type_info& info1a = typeid(add_cons t_ref<int>::typ e);

where the compiler does not like the ::type in the call to typeid.

The error is :

error C2955: 'add_const_ref_ impl<false>::ty pe_' : use of class
template requires template argument list
see declaration of 'add_const_ref_ impl<false>::ty pe_'
see reference to class template instantiation 'add_const_ref< T>' being
compiled
with
[
T=int
]

I’m obviously don’t understand what is going on here. I would expect
that buy the time the compiler has done its meta programming magic
add_const_ref<i nt>::type would be a real type and I could pass it into
typeID, however that looks wrong. I think I'm misunderstandin g
something fundermetal here so I'm posting.

So my question is why does this line fail to compile. Should I be able
to pass add_const_ref<i nt>::type into typeid()?

Thanks

stdlib

Ps. sorry if this is in the wrong group!
//=============== =============== =============== =======

// This is an answer to they very first exercise in the
// C++ Template Metaprogramming book.
// Its the impl of a function add_const_ref function which add adds
converts a supplied type into
// a const ref to that type.

#include <boost/type_traits/is_reference.hp p>
#include <iostream>

template <bool is_refstruct add_const_ref_i mpl
{};
template <>
struct add_const_ref_i mpl<false>
{
template<typena me T>
struct type_
{
typedef const T& type;
};

};

template <>
struct add_const_ref_i mpl<true>
{
template<typena me T>
struct type_
{
typedef T type;
};

};
//=============== =============== ============
template <class T>
struct add_const_ref
{
static const bool is_ref = boost::is_refer ence<T>::value;
typedef typename add_const_ref_i mpl<is_ref>::ty pe_::type type;
};
int main()
{
const type_info& info1a = typeid(add_cons t_ref<int>::typ e); //
error C2955 here
const type_info& info2a = typeid(add_cons t_ref<int&>::ty pe); //
error C2955 here

std::cout << info1a.name() << std::endl << info2a.name() <<
std::endl;

return 0;
}
//=============== =============== =============== =======
Sep 1 '08 #1
3 1852
On Sep 1, 9:38*pm, stdli...@google mail.com wrote:
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 Gurtovoy. I’m
not doing this because I want to be a Meta Programming guru (because a
lot of that stuff looks too crazy for use in the real world).

Rather I want to learn heavyweight templates and this is the only
hardcode template book that has questions and answers (on the web).

Anyway I have got as far as the first exercise. *I have done the
exercise but I’m getting a compile error below.

I’m using Visual Studio 2005, but I’m sure its not a compiler problem
– its just I don’t understand what is going on.

The compile error is on *the line (full code below):

* const type_info& info1a = typeid(add_cons t_ref<int>::typ e);

where the compiler does not like the ::type in the call to typeid.

The error is :

error C2955: 'add_const_ref_ impl<false>::ty pe_' : use of class
template requires template argument list
see declaration of 'add_const_ref_ impl<false>::ty pe_'
see reference to class template instantiation 'add_const_ref< T>' being
compiled
* * * * with
* * * * [
* * * * * * T=int
* * * * ]

I’m obviously don’t understand what is going on here. I would expect
that buy the time the compiler has done its meta programming magic
add_const_ref<i nt>::type would be a real type and I could pass it into
typeID, however that looks wrong. I think I'm misunderstandin g
something fundermetal here so I'm posting.

So my question is why does this line fail to compile. Should I be able
to pass add_const_ref<i nt>::type into typeid()?

Thanks

stdlib

Ps. sorry if this is in the wrong group!

//=============== =============== =============== =======

// This is an answer to they very first exercise in the
// C++ Template Metaprogramming book.
// Its the impl of a function add_const_ref function which add adds
converts a supplied type into
// a const ref to that type.

#include <boost/type_traits/is_reference.hp p>
#include <iostream>

template <bool is_refstruct add_const_ref_i mpl
{};

template <>
struct add_const_ref_i mpl<false>
{
* template<typena me T>
* struct type_
* {
* * typedef const T& type;
* };

};

template <>
struct add_const_ref_i mpl<true>
{
* template<typena me T>
* struct type_
* {
* * typedef T type;
* };

};

//=============== =============== ============

template <class T>
struct add_const_ref
{
* static const bool is_ref = boost::is_refer ence<T>::value;
* typedef typename add_const_ref_i mpl<is_ref>::ty pe_::type type;

};

int main()
{
* const type_info& info1a = typeid(add_cons t_ref<int>::typ e); *//
error C2955 here
* const type_info& info2a = typeid(add_cons t_ref<int&>::ty pe); //
error C2955 here

* std::cout << info1a.name() << std::endl << info2a.name() <<
std::endl;

* return 0;}

//=============== =============== =============== =======
I can see two things here:

First, when you reference add_const_ref_i mpl<is_ref>::ty pe_::type in
add_const_ref, noticing add_const_ref_i mpl::type_ is also a template,
you should provide a template argument here too. This is what the
compiler complains about.

Second, in the statement typeid(add_cons t_ref<int>::typ e),
add_const_ref<i nt>::type will cause ambitious, you should add a
'typename' here:
typeid(typename add_const_ref<i nt>::type)
Sep 1 '08 #2


st******@google mail.com wrote:
template <class T>
struct add_const_ref
{
static const bool is_ref = boost::is_refer ence<T>::value;
typedef typename add_const_ref_i mpl<is_ref>::ty pe_::type type;
try this:

typedef typename add_const_ref_i mpl<is_ref>::te mplate type_<T>::type type;
};
HTH,
jim
Sep 2 '08 #3
On Sep 2, 8:06 am, "Jim Z. Shi" <ji...@cisco.co mwrote:
stdli...@google mail.com wrote:
template <class T>
struct add_const_ref
{
static const bool is_ref = boost::is_refer ence<T>::value;
typedef typename add_const_ref_i mpl<is_ref>::ty pe_::type type;

try this:

typedef typename add_const_ref_i mpl<is_ref>::te mplate type_<T>::type type;
};

HTH,
jim
Hi,

Thanks to both of you for taking the time to reply.

The compile errors were indeed becase of the
missing template argument for type_<T>. Id did not have to use the
template Keyword
as VisualStudio 2008 was able to itendify the type_T<Tas a teample.

I as so made a mistake with the use of typeid():

I was expecting info1a.name() and info2a.name() to be different where
info2a.name would
be a const int&. But that is not the case as typeid() discards all the
const/ref info
so typeid(const int&) == typeid(int).
HTH,
jim
Sep 10 '08 #4

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

Similar topics

0
1256
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 compile time. It works, but if my approach is more awkward or "bad" than need be, I'd love to hear suggestions for improvement! #include <iostream>
12
2159
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 code)? It's not a perfect analogy, but it seems to be a reasonable statement...
5
1721
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
9
1573
by: Rock Johnson | last post by:
I understand that template metaprogramming is a technique that allows for calcualations to occur at compile-time rather than run-time. Can someone explain what is the benefit of this, and when it is good to employ Template Metaprogramming?
7
3532
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 metaprogramming is supported in C# (.NET)? For reference I found it: http://en.wikipedia.org/wiki/Template_metaprogramming. Thanks to all.
4
1901
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 problem here. Comeau online (without C++0x, in strict mode) accepts this example pasted here, (apologies for the length of it). Unfortunately G++ (3.4, 4.1, 4.2) doesn't, and complains about index_find being private. The exact error it gives is:...
5
3620
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 anyone explain why loop unrolling doesn't play well with templates? Or better, can someone submit a code fragment to get desired results? Here are the command-lines I use to generate code: "g++ -DTEMPLATE=0 -o gotofun0 gotofun.cpp" works exactly as...
4
1865
by: suman.nandan | last post by:
Hi C++ Experts ! I have a little weird requirement. I have a base class, say B and lots of classes D1 .. Dn publicly derived from it. Over the course of development the number of derived classes may increase. I want that before the execution of one of my particular code, I should have a list of pointers to all the derived classes, say std::list<B*ptrList; // contains 'n' elements which are pointers to D1 .. Dn.
4
254
by: nooneinparticular314159 | last post by:
I'm trying to understand template metaprogramming syntax. It's been years since I've touched C++, so this may actually be a C++ syntax issue (although it seems that the language has changed somewhat.) The following example comes from http://www.codeproject.com/KB/cpp/crc_meta.aspx template< int i > class FACTOR{ public: enum {RESULT = i * FACTOR<I-1>::RESULT};
0
8787
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
9334
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
9259
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,...
1
6750
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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
4569
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...
1
3279
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
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.