 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Guest
|
Posted: Sat May 19, 2007 12:04 am Post subject: Optimized out? |
|
|
Just a quick compile time, template, optimized out question for anyone
that has time to answer this.
Let's say I define some template class like such:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename B>
struct CaseOneCheck
{
private:
template<typename C> static One DepTest(typename C::CaseOne *);
template<typename C> static Two DepTest(...);
public:
enum { Dependent = sizeof(CaseOneCheck<B>::DepTest<B>(0)) ==
1 };
typedef double Type;
inline static bool Check(Type amount)
{
return(amount > 0);
}
};
Where I define:
struct WidgetOne
{
typedef bool CaseOne;
};
struct WidgetTwo
{
typedef bool CaseTwo;
};
Then, a method:
template<typename Arg,
template<typename> class CheckPolicy>
bool ShouldEnable(typename CheckPolicy<Arg>::Type type)
{
bool returnCode = true;
if(CheckPolicy<Arg>::Dependent)
{
returnCode = CheckPolicy<Arg>::Check(type);
}
return returnCode;
}
Will, since I am assuming the if(CheckPolicy<Arg>::Dependent) is
filled in to true or false at compile time, then cause the compiler to
optimize out the returnCode = CheckPolicy<Arg>::Check(type); line if
the if is false at the "second" compile stage? Or will it always be
there? The compiler I am using is g++ version 3.3.3.
That is, depending on the dependence, the code will be
ShouldEnable()
{
bool returnCode = true;
return returnCode;
}
When I use:
bool test = ShouldEnable<WidgetTwo, CheckOne>(5.5);
?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Mathias Gaunard Guest
|
Posted: Sat May 19, 2007 6:03 pm Post subject: Re: Optimized out? |
|
|
On May 19, 2:04 am, Chris <foureightye...@yahoo.com> wrote:
| Quote: | Will, since I am assuming the if(CheckPolicy<Arg>::Dependent) is
filled in to true or false at compile time, then cause the compiler to
optimize out the returnCode = CheckPolicy<Arg>::Check(type); line if
the if is false at the "second" compile stage? Or will it always be
there? The compiler I am using is g++ version 3.3.3.
|
Try it and see.
Look at the asm output.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Tue May 22, 2007 12:10 am Post subject: Re: Optimized out? |
|
|
On May 19, 2:04 am, Chris <foureightye...@yahoo.com> wrote:
| Quote: | template<typename Arg,
template<typename> class CheckPolicy
bool ShouldEnable(typename CheckPolicy<Arg>::Type type) {
bool returnCode = true;
if(CheckPolicy<Arg>::Dependent) {
returnCode = CheckPolicy<Arg>::Check(type);
}
return returnCode;
}
Will, since I am assuming the if(CheckPolicy<Arg>::Dependent) is
filled in to true or false at compile time, then cause the compiler to
optimize out the returnCode = CheckPolicy<Arg>::Check(type); line if
the if is false at the "second" compile stage?
|
Perhaps. C++ allows both. C++ even (to a degree) allows interpreters,
in
which case the entire question is even fuzzier.
For many compilers, the answer is still "it depends"; compiler flags
do
matter. Many have so-called debug modes in which no code is ever
optimized.
The line of code must still compile, though. SFINAE doesn't work on a
line-by-line basis.
HTH,
Michiel Salters
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|