Boost logo

Boost Users :

Subject: [Boost-users] [mpl] Any mpl tricks to use default tags and get around function template argument deduction with function defaults?
From: jesseperla (jesseperla_at_[hidden])
Date: 2009-07-10 10:12:33


I am building a make_ function for a complicated type with many
generic parameters that all have defaults.
I believe the following doesn't work in C++03 (but might in C++0X?)
template<class T> void myfunc(T t = default_type()) {} if called with
no arguments.

Here is pseudocode for what I would like to do (unworking, so please
bear with any mpl mistakes)

struct default_tag{};
struct default_type // metafunction to return the default type to keep
it centralized.
{
   typedef int type;

   type construct() //A construction function?
   {
        return type();
    }
};

template<class T>
class myclass
{
   myclass(const T& t){....} //Requires a type
};

template<class T>
myclass< mpl::if_<mpl::is_same<T, default_tag>, //returns a my_class<>
                           default_type, //call this metafunction if
default
                           T > //otherwise pass it in.
>
       make_myclass(const T& t = default_tag()) //Note use of the
default_tag
{
              typename myclass< mpl::if_<mpl::is_same<T, default_tag>,
 
default_type, //call this metafunction
                                                         T >::type
resolved_type; //this is what we want to use

              //Here I am confused of how to conditionally construct a
type of default_type::type to pass in?
             // I thought it might help to have a
default_type.construct function but wasn't sure what to do?
             //Note that T is often too large to do a full 'copy'. It
won't be an int.

              //Pass in t directly or a constructed version of
resolved_type if T if 'default'?
              return my_class<resolved_type>( ...);

}

The usage I would like to enable:
auto m1 = make_myclass(); //Uses the default
auto m2 = make_myclass( default_tag() ); //Uses the default as well.
Why do I need this? Will actually have a number of these tags where
you may want to conditionally choose some as defaults.

int n = 5;
auto m3 = make_myclass(n);

Any ideas on the best idioms to support this and get around the
template argument detection problems? Is this possible?

If not, can I drop the need for the make_myclass(); usage and still
keep the ability to call with a default and conditionally constructs
the default object based ont eh type of a metafunction?

Thanks,
Jesse


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net