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

overloading on the template parameter arity of a template templateparameter

// I think that there is no way to write the template "cant_write_me" in
// this example. Would love to be wrong.

// one of many approaches that won't work
template< template< typename class struct cant_write_me{};
template< template< typename, typename class struct cant_write_me{};

template< typename struct arity_one{};
template< typename, typename struct arity_two{};

cant_write_me< arity_one instantiated_arity_one;
cant_write_me< arity_two instantiated_arity_two;
Jul 19 '06 #1
4 1795
Howard Gardner wrote:
// I think that there is no way to write the template "cant_write_me"
in // this example. Would love to be wrong.

// one of many approaches that won't work
template< template< typename class struct cant_write_me{};
template< template< typename, typename class struct
cant_write_me{};
'cant_write_me' here cannot be used because it's not a specialisation of
the original template. The name has to be unique or it has to be some
kind of specialisation (which requires the <after the name).
template< typename struct arity_one{};
template< typename, typename struct arity_two{};

cant_write_me< arity_one instantiated_arity_one;
cant_write_me< arity_two instantiated_arity_two;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '06 #2
Victor Bazarov wrote:
Howard Gardner wrote:
>// I think that there is no way to write the template "cant_write_me"
in // this example. Would love to be wrong.

// one of many approaches that won't work
template< template< typename class struct cant_write_me{};
template< template< typename, typename class struct
cant_write_me{};

'cant_write_me' here cannot be used because it's not a specialisation of
the original template. The name has to be unique or it has to be some
kind of specialisation (which requires the <after the name).
>template< typename struct arity_one{};
template< typename, typename struct arity_two{};

cant_write_me< arity_one instantiated_arity_one;
cant_write_me< arity_two instantiated_arity_two;

V
I know :)

I've tried a large number of approaches that use specializations. I've
gotten very creative. I think that there simply is no way to write
cant_write_me.

I ran across it because I was trying to write a facility to use like this:

template_arity_of< class-template-identifier >::value

I did in fact manage to solve that problem, but the solution is pretty
unloveable. I have to write

template_arity_of
<
MACRO( class-template-identifier )
>::value
that solution looks like:

#include <ostream>
#include <cstddef>

typedef char no;
struct yes{ no value[2]; };

template< template< typename class >
yes test_1();

template< template< typename, typename class >
no test_1();

template< template< typename class >
no test_2();

template< template< typename, typename class >
yes test_2();

template< size_t, size_t >
struct template_arity_of;

template< >
struct template_arity_of< sizeof( yes ), sizeof( no ) >
{
static const size_t value = 1;
};

template< >
struct template_arity_of< sizeof( no ), sizeof( yes ) >
{
static const size_t value = 2;
};

#define MACRO( template_class_id ) \
sizeof( test_1< template_class_id >() ),\
sizeof( test_2< template_class_id >() )

template< typename struct arity_one;
template< typename, typename struct arity_two;

int
main()
{
using namespace std;
cout << template_arity_of< MACRO( arity_one ) >::value << endl;
cout << template_arity_of< MACRO( arity_two ) >::value << endl;
}

There're two really vile problems with this solution.

First, if N is the largest arity that I test for, implementing this
solution requires 2*N*N + N bits of syntax.

Second, there's the macro. It's there for three reasons. First, a
straight use requires N bits of syntax. Second, those bits of syntax are
very likely to contain a bug. Third, it's pretty obvious that N will
grow and it would be bad to have to rewrite all the uses.

I'm looking into another variation of the sizeof trick (sometimes used
for a restricted typeof) which would let me implement a less hateful
kludge, but I'd much rather learn that there's some way to write
"cant_write_me" after all.
Jul 19 '06 #3
Howard Gardner wrote:
template_arity_of
<
MACRO( class-template-identifier )
>::value
What about

TEMPLATE_ARITY_OF(class-template-identifier)

?

This is almost a religious issue. People hate macros, but I think the
reasons are related to macro usage that hides the runtime code. In a
compile-time facility like this, macros compliment template
meta-programming in the areas where it's not that great -- variable
number of template/function parameters.
First, if N is the largest arity that I test for, implementing this
solution requires 2*N*N + N bits of syntax.
Just use the Boost.Preprocessor library and have it generate these bits
of syntax for you.

Regards,
Arkadiy

Jul 19 '06 #4
Arkadiy wrote:
Howard Gardner wrote:
>template_arity_of
<
MACRO( class-template-identifier )
> >::value

What about

TEMPLATE_ARITY_OF(class-template-identifier)

?

This is almost a religious issue. People hate macros, but I think the
reasons are related to macro usage that hides the runtime code. In a
compile-time facility like this, macros compliment template
meta-programming in the areas where it's not that great -- variable
number of template/function parameters.
>First, if N is the largest arity that I test for, implementing this
solution requires 2*N*N + N bits of syntax.

Just use the Boost.Preprocessor library and have it generate these bits
of syntax for you.
I sure don't want to fight a battle in the macros holy war :)

I prefer not to use them, but sometimes using them is better. I think
that was a pretty clear case, and it was even clearer in the real code
where N was 8 right off the bat.

I macrod the template parameter syntax rather than the whole invocation
because that was enough to encapsulate the cruft.

I didn't use the Boost.Preprocessor library because I don't know it.
When I do eventually grapple with it, it will probably be because I want
to read the boost source code (Function, Lambda, TypeTraits, and MPL)
and not because I actually want to use it.

I got the other solution working. It looks like this:

#include <ostream>
#include <cstddef>

typedef char t01[1];
template< template< typename class >
t01 * test();

typedef char t02[2];
template< template< typename, typename class >
t02 * test();

template< size_t xSize >
struct template_arity_of
{
static const size_t value = xSize;
};

template< typename struct arity_one;
template< typename, typename struct arity_two;

int
main()
{
using namespace std;

cout
<< template_arity_of< sizeof( * test< arity_one >() ) >::value
<< endl;

cout
<< template_arity_of< sizeof( * test< arity_two >() ) >::value
<< endl;
}

It's better, but the invocation syntax is still ugly. It's tempting to
macro it, but in this case I'll probably refrain.

The situation is different than it was because the old reasons don't
apply anymore:

1) It's not horrifically long.

2) It's not particularly likely that someone will accidentally write
something that compiles but doesn't work.

3) Even though N will grow, the existing syntax won't have to change.

4) (I failed to mention this when I was apologizing for the macro in the
previous rendition) I don't think I'd use the macro personally.

The new temptation actually comes from the fact that I WANT to change
the invocation syntax, and will do it the very INSTANT that I can find
better syntax.
Jul 19 '06 #5

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

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: Arkadiy Vertleyb | last post by:
Hi all, I am having a problem trying to overload a function template, based on a typedef, such as: template<class T> struct A {}; template<class T>
3
by: Gianni Mariani | last post by:
I was a little surprised by this: It seems like the code below should not compile but the Comeau 4.3.3 compiler accepts it and the gcc 3.4(prerel) compiler rejects it and MSVC++7.1 ICE's. ...
7
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
5
by: Bolin | last post by:
I am frustrated as I am trying to design a toy image library. I want the user to write mul(im, 2) if s/he wants to multiply the image by 2, and mul(im1, im2) if s/he wants to multiply...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
35
by: josh | last post by:
Hi, I coded the following but It does not return what I expect, why? #include <iostream> using namespace std; class Other { public: int i;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.