views:

85

answers:

0

i am interested to synthesize a few classes which will have some/all of the functions as in the class res_obj.

struct res_obj
{
    int* res_;
    res_obj() ///method 1 : no arg constructor
        : res_(0)
    {
    }
    explicit res_obj(int value) ///method 2 : single arg constructor
            : res_(new int(value))
    {
    }
    res_obj(const res_obj& rhs)
        : res_(new int(rhs.res_))///method 3 : copy constructor.
    {
    }
    res_obj(const move_object<res_obj>& rhs) 
      : res_(rhs.source.res_)///method 4: an in-house move constructor
    {
       rhs.source.res_ = 0;
    }
    void swap(res_obj& other) ///method 5 : swap
    {
       std::swap(res_,other.res_);
    }
};

So far i have all of those functions as macros, in in my class i enable/disable them as required like

struct res_obj1
{
   ENABLE_SINGLE_ARG_CTOR(res_obj1)
   DISABLE_COPY_CTOR(res_obj1)
};

However, i am looking for a template based mixin where i can put those parts freely to construct new objects, using specialization, without repeating any implementation including constructors. For the constructors i can follow a pattern like http://www.codeproject.com/KB/tips/FakeTemplate.aspx and for enabling/disabling copy constructor the CRTP ideom, but having the same for swap etc and combining them together in a single solution is what i am looking for. The condition for the generated objects are that, they should not leave any members other than the one compiler understands as unimplemented. So, for an object with no copy will be generated as

struct res_nocopy
{
  res_nocopy(const res_nocopy& rhs);//either public or private
};

but for an object with no swap will be

struct res_noswap
{
};

rather than,

struct res_noswap
{ 
  swap(res_noswap& rhs);//no implementation
};

as i need to detect existence of those functions automatically.

i am interested to know if it is a possible to do using template specialization, and to what extent.


NOTE :

  • intentionally omitted the assignment operator & move assignment operator to make it simpler.
  • have no restrictions on how the classes will be generated
  • The generated class have only the single member variable res_ , and all of the generated classes will be used as value object.