Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multiple headers, but one function body

Reply
Thread Tools

Multiple headers, but one function body

 
 
PGK
Guest
Posts: n/a
 
      09-08-2010
Hi all,

I'm porting a library to a cpp research compiler. Unfortunately
compiler support for templates is limited and I must create
specialised versions of each template function I come across. This
ends up with a lot of code duplicated by hand. I'm looking for a quick
way to do this automatically, that I can easily remove; likely in a
few weeks.

A toy example: Given,

template <typename T> void foo(T x) { std::cout << x <<
std::endl; }

I must create:

template <> void foo(int x) { std::cout << x << std::endl; }

template <> void foo(float x) { std::cout << x << std::endl; }

template <> void foo(double x) { std::cout << x << std::endl; }

As there are often only a few different declarations, all with the
same function bodies, I'd like to avoid copying and pasting by hand.
Ideally I could do something like:

template <>
void foo(int x), void foo(float x), void foo(double x)
{ std::cout << x << std::endl; }

A function object, or proxy function will not work, as it too will
need similar multiple function bodies.

Perhaps I should define a cpreprocessor macro, but this will require
putting newline tokens at the end of each line of all (often long)
function bodies.

Are there any better methods?

Thanks,
Graham
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2010
On 9/8/2010 7:46 AM, PGK wrote:
> I'm porting a library to a cpp research compiler. Unfortunately
> compiler support for templates is limited and I must create
> specialised versions of each template function I come across. This
> ends up with a lot of code duplicated by hand. I'm looking for a quick
> way to do this automatically, that I can easily remove; likely in a
> few weeks.
>
> A toy example: Given,
>
> template<typename T> void foo(T x) { std::cout<< x<<
> std::endl; }
>
> I must create:
>
> template<> void foo(int x) { std::cout<< x<< std::endl; }
>
> template<> void foo(float x) { std::cout<< x<< std::endl; }
>
> template<> void foo(double x) { std::cout<< x<< std::endl; }
>
> As there are often only a few different declarations, all with the
> same function bodies, I'd like to avoid copying and pasting by hand.
> Ideally I could do something like:
>
> template<>
> void foo(int x), void foo(float x), void foo(double x)
> { std::cout<< x<< std::endl; }
>
> A function object, or proxy function will not work, as it too will
> need similar multiple function bodies.
>
> Perhaps I should define a cpreprocessor macro, but this will require
> putting newline tokens at the end of each line of all (often long)
> function bodies.


You mean the backslashes? What's the big deal? If you're going to
reformat your code automatically using some kind of processing script
(like Perl or Awk), then you just write two scripts, one for direct
processing and the other for reversing the changes. Mark those added
elements (like the backslashes) with preceding comments in /**/ with
some key sequence of symbols that your script would recognize...

> Are there any better methods?


You mean, better standard C++ methods to work around a non-standard C++
compiler?...

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hide whole body but one div czechboy Javascript 5 07-03-2008 07:27 AM
To reduce your body weight & slim your body Loss weight MCSA 0 07-23-2007 07:54 PM
To reduce your body weight & slim your body Loss weight MCSA 0 07-21-2007 05:15 AM
Multiple asp:buttons on one form but ONLY one should submit? D. Shane Fowlkes ASP .Net 3 02-24-2004 12:17 PM
Not detecting body.scrollTop and body.scrollLeft in IE6 London Boy Javascript 2 01-12-2004 08:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57