473,401 Members | 2,127 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,401 software developers and data experts.

More template metaprogramming fun.... (G++/Comeau disagree)

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:

test.cc:21: error: 'template<class T, class Ntemplate<class X, class
Oclass typelist<T, N>::index_find' is private
test.cc:21: error: within this context

Which seems to imply I've gotten the friend declaration wrong. Is my
syntax correct for this? Can somebody point me in the direction of a
definitive answer on this one? Obviously if I make index_find public
then this whole problem goes away, but given that it's part of the
internal implementation mechanism, rather than the public interface I'd
rather keep it private, and hopefully learn which compiler is
right/wrong in this instance. I was quite surprised g++ rejected this
when Comeau accepts it.

Thanks,
Alan

#include <typeinfo>
#include <cassert>

// Utility for comparing two types
template <typename A, typename B>
class compare_types {
public:
enum { is_same = 0 };
};

// and corresponding the partial specialisation
template <typename A>
class compare_types<A,A{
public:
enum { is_same = 1 };
};

template <typename T, typename N=void>
class typelist {
template <typename X, typename O>
class index_find;

template <typename U, typename M>
template <typename X, typename O>
friend class typelist<U,M>::index_find;
public:
typedef T type;
typedef N next;

/**
* Constant value used to indicate that a type was not found during
a search of a typelist
*/
enum { not_found=~0 };

private:
template <typename X, typename O>
class index_find {
public:
// If we are a match end recursion with a 0.
// If we aren't a match carry on recursively searching.
// If our recursive search returns failure then we should return
failure also.
enum { index=compare_types<X,T>::is_same ? 0 :
((static_cast<int>(N::template index_find<X, typename N::next>::index)
!= static_cast<int>(not_found))
? 1 + N::template index_find<X, typename N::next>::index
: not_found) };
};

template <typename X>
class index_find<X, void{
public:
// If we are a match end recursion with a 0 index.
// If we aren't a match we can't recurse any further since we
are the tail, so propagate not_found back.
enum { index=compare_types<X,T>::is_same ? 0 : not_found };
};
public:
/**
* Use this to search to see if a typelist contains a specified
type. Returns the index
* it was (first if a type appears more than once) found at, or
not_found otherwise.
*/
template <typename X>
class index_of {
public:
enum { index=index_find<X,N>::index };
};
};

// make sure we instantiate the templates here:
int main() {
typedef typelist<float, typelist<int, typelist<char list1;
assert(static_cast<int>(list1::not_found) ==
static_cast<int>(list1::index_of<double>::index));
assert(0 == static_cast<int>(list1::index_of<float>::index));
assert(1 == static_cast<int>(list1::index_of<int>::index));
assert(2 == static_cast<int>(list1::index_of<char>::index));

return 0;
}
Oct 25 '07 #1
4 1875
Your program is pathetic try writing some better programs I tried it
out and i doesn't work very well .
if you're not serius about this then i suggest you leave this group.
I want this group to have members who are excellent in the c++
programing language okay so if you want to remain a member write a
better program.

Oct 25 '07 #2
Hi

Alan Woodland wrote:
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:

test.cc:21: error: 'template<class T, class Ntemplate<class X, class
Oclass typelist<T, N>::index_find' is private
test.cc:21: error: within this context

Which seems to imply I've gotten the friend declaration wrong. Is my
syntax correct for this? Can somebody point me in the direction of a
definitive answer on this one? Obviously if I make index_find public
then this whole problem goes away, but given that it's part of the
internal implementation mechanism, rather than the public interface I'd
rather keep it private, and hopefully learn which compiler is
right/wrong in this instance. I was quite surprised g++ rejected this
when Comeau accepts it.

Thanks,
Alan

template <typename T, typename N=void>
class typelist {
template <typename X, typename O>
class index_find;

template <typename U, typename M>
template <typename X, typename O>
friend class typelist<U,M>::index_find;
The problem seems to be here already.
Apparently, the code should not compile, because typelist<U,M>::index_find
is not accessible within typelist<T,N>, unless T=U and N=M. Therefore, the
friend declaration is ill-formed according to 11.4(7)
There was a Defect Report about this:
http://www.open-std.org/jtc1/sc22/wg...losed.html#209

The other problem is, even if that was allowed, I don't see if members of
templates can be friends of templates... 14.5.3(6) explicitly talks about
non-templates only, but I might misinterpret that:
"A member of a class template may be declared to be a friend of a
non-template class."

However, GCC accepts the code if you just make other typelists friends, so

template<typename U, typename M>
friend class typelist;

I'm not sure if the code _should_ be accepted, though.

Markus

Oct 25 '07 #3
REH
On Oct 25, 9:40 am, cplusplus <zapp...@gmail.comwrote:
Your program is pathetic try writing some better programs I tried it
out and i doesn't work very well .
if you're not serius about this then i suggest you leave this group.
I want this group to have members who are excellent in the c++
programing language okay so if you want to remain a member write a
better program.
Who are you to dictate who is allowed in this group? If we were all
*excellent* in C++, there would be no need to even have a group.
Oct 25 '07 #4
>template <typename T, typename N=void>
>class typelist {
template <typename X, typename O>
class index_find;

template <typename U, typename M>
template <typename X, typename O>
friend class typelist<U,M>::index_find;

The problem seems to be here already.
Apparently, the code should not compile, because typelist<U,M>::index_find
is not accessible within typelist<T,N>, unless T=U and N=M. Therefore, the
friend declaration is ill-formed according to 11.4(7)
There was a Defect Report about this:
http://www.open-std.org/jtc1/sc22/wg...losed.html#209
Does this imply Comeau is in error for accepting this?
The other problem is, even if that was allowed, I don't see if members of
templates can be friends of templates... 14.5.3(6) explicitly talks about
non-templates only, but I might misinterpret that:
"A member of a class template may be declared to be a friend of a
non-template class."

However, GCC accepts the code if you just make other typelists friends, so

template<typename U, typename M>
friend class typelist;

I'm not sure if the code _should_ be accepted, though.
The final paragraph of the defect report you linked to does seem to
imply that that should indeed be accepted, provided reading template
class instead of function is reasonable.

"It was observed that most of the problems involved with the current
state of affairs result from inability to declare a particular member
function as a friend; in such cases, an easy workaround is simply to
befriend the entire class rather than the specific member function."

Unfortunately it seems G++ 3.4, doesn't share this view so I think I'm
just going to have to go ahead and make index_find public against my
better judgement.

Alan
Oct 26 '07 #5

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

Similar topics

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...
5
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!
10
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
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...
8
by: IR | last post by:
Hello, Does anyone know why this declaration compiles: template< template<typenameclass T> class X { /*...*/ }; while this one doesn't: template< template<typenametypename T>
9
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...
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
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
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
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
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...
0
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...
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.