473,854 Members | 1,783 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 1859
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
1261
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
2167
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
1728
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
1582
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
3585
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
1909
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
3625
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
1869
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
9750
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
11024
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10740
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
10362
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
7076
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
5738
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
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
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
3
3182
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.