templated function default argument that does nothing

H

Hicham Mouline

Hello,

I have a function template whose arguments specify policy inside a tight
loop

struct DefaultPolicy {
void operator()(...)
{
}
};


struct OtherPolicy{
void operator()(...)
{
// do something
}
};

template <typename T1, typename Policy>
void f( const T1& t1, const Policy& policy = DefaultPolicy() )
{

for (...)
for (...)
{
// get first result
policy ( result );
}
}

}


so that the user can call f without specifying the template arguments,
just the T1 arg, and sometimes the policy arg too.

Ideally, the policy ( result ) call would be totally optimized away by the
compiler if it's the default one.

Does this seem a reasonable pattern?

regards,
 
S

SG

Hello,

I have a function template whose arguments specify policy inside a tight
loop
[...]

Does this seem a reasonable pattern?

Does it even work? I don't think so.

Cheers!
SG
 
H

Hicham Mouline

SG said:
Hello,

I have a function template whose arguments specify policy inside a tight
loop
[...]

Does this seem a reasonable pattern?

Does it even work? I don't think so.

Cheers!
SG
Turns out I have to specify the template argument Policy in all cases, even
if I want the default.
Also, function template cannot have default args.

Nevertheless, how about the optimization away questions?

regards,
 
R

red floyd

Hello,
I have a function template whose arguments specify policy inside a tight
loop
Does this seem a reasonable pattern?
Does it even work? I don't think so.
Cheers!
SG

Turns out I have to specify the template argument Policy in all cases, even
if I want the default.
Also, function template cannot have default args.

Nevertheless, how about the optimization away questions?

I believe Alexandrescu uses this pattern in MC++D.
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top