C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reducing code duplication

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Temirgaleev Eugene
Guest





PostPosted: Fri Sep 24, 2004 10:19 pm    Post subject: Reducing code duplication Reply with quote



Good time of the day (or night)!

//=====================================================================
Reducing code duplication.
//---------------------------------------------------------------------
This is not a trouble. This is interesting theoretical question (for me).
Therefor, don't spent your time reading/answering this, if you have
some important things to do.

Let we have a template class with more than one type argument.
For example, two. There is non-inline static member function,
depending on only one template argument. Let us assume that on
first.

template <typename T1, typename T2>
class C {
...
static void F(T1 x, T1 y, int z);
};

template <typename T1, typename T2>
void C<T1, T2>::F(T1 x, T1 y, int z)
{
// T2 is never used
....
}

Let P and Q - set of types. Suppose, that there are no specializations
C<p, q>, there p in P and q in Q. Now, by fixing p0 in P, we get for
any q1, q2 in Q different functions:
C<p0, q1>::F
C<p0, q2>::F
which will be instantiated from primary template C and must be the same.

I use BCB6. In same example it's easy procude two different instances,
which are equal line by line in machine code. So it's possible to merge them
into one.

My questions:
1) Is it something about merging these instances in C++ standard?
2) Are there compilers, which can do so?

//=====================================================================

Thanks in advance. Sorry for bad english.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Sun Sep 26, 2004 4:06 pm    Post subject: Re: Reducing code duplication Reply with quote



Temirgaleev Eugene wrote:
<snip>
Quote:
Let we have a template class with more than one type argument.
For example, two. There is non-inline static member function,
depending on only one template argument.
snip
I use BCB6. In same example it's easy procude two different instances,
which are equal line by line in machine code. So it's possible to merge them
into one.

My questions:
1) Is it something about merging these instances in C++ standard?

Each function must have a different address, so if the program takes
the addresses of the two instances and compares them, they must
compare unequal. However, so long as the program doesn't do that, the
as-if rule means they can be merged. (In fact, even if the program
does compare them, the implementation could generate extra entry
points that simply branch to the merged code, and give the addresses
of those.)

Quote:
2) Are there compilers, which can do so?

Visual C++ has provided the option to do this at least since version
7.0. It is enabled by the linker option "/opt:icf". Unfortunately it
does not ensure that the addresses of merged functions compare
unequal.

--
Ben Hutchings
Make three consecutive correct guesses and you will be considered an expert.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jozef Saniga
Guest





PostPosted: Mon Sep 27, 2004 10:35 am    Post subject: Re: Reducing code duplication Reply with quote



Eugene,

Temirgaleev Eugene wrote:

<snip>

Quote:
template <typename T1, typename T2
class C {
...
static void F(T1 x, T1 y, int z);
};

template void C {
// T2 is never used
...
}

<snip>

You could try to refactor your code and make C::F a static function of a
different class or a freestanding function:

template <typename T1>
class D {
static void F(T1 x, T1, y, int z);
};

template <typename T1, typename T2>
class C {
...
// uses D<T1>::F
...
};

This way C<p0,q1> and C<p0,q2> will use just one instantiation of D::F
instead of two identical ones.

Jozef

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.