templated function default argument that does nothing

Discussion in 'C++' started by Hicham Mouline, Mar 17, 2009.

  1. 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,
     
    Hicham Mouline, Mar 17, 2009
    #1
    1. Advertisements

  2. Hicham Mouline

    SG Guest

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

    Cheers!
    SG
     
    SG, Mar 17, 2009
    #2
    1. Advertisements

  3. 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,
     
    Hicham Mouline, Mar 17, 2009
    #3
  4. Hicham Mouline

    red floyd Guest

    I believe Alexandrescu uses this pattern in MC++D.
     
    red floyd, Mar 17, 2009
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.