Multiple headers, but one function body

P

PGK

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
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top