Page 1 of 1

Complex problem with c++ templates

Posted: Mon Apr 30, 2007 2:18 pm
by Candy
I've been messing with in particular using template template parameters to specify policy for a class. I've created a rational class (as per Mystran's suggestion elsewhere) for holding rational variables and I've added a policy that allows you to determine what to happen when you try to convert it to a different value type before the programs creates a crash condition (divide by 0, overflow, those things). I've added a do_nothing class (templatized on the type), a create_exception_on_divide_by_zero_attempt and I'm trying to create a default_value class, but I can't figure out how to pass it to the class.

The code that contains the problem:

Code: Select all

template <typename int_t> class empty_policy {};

template <typename int_t, int value> class default_value {};

template <typename int_t, template <typename> class policy>
class rational {};

int main() {
    rational<int, ???? > x;
}
What do I put at ???? to pass a partial specialization of default_value? I've tried

Code: Select all

    rational<int, template<typename T> checks::default_value<T, 22>  > x;
but GCC tripped over the T.

If I could make a template typedef you could do

Code: Select all

typedef template <typename T> checks::default_value<T, 22> default_value_of_22;
rational<int, default_value_of_22> x;
but template typedefs don't exist.


Anybody?

Posted: Mon Apr 30, 2007 2:24 pm
by Candy
I got a method that works but it's fugly.

Code: Select all

template <typename i>
class default_22 : public checks::default_value<i, 22> {};

int main() {
   rational<int, default_22> x;
}
Any other ideas?

Posted: Mon Apr 30, 2007 3:22 pm
by mystran
What exactly are you trying to do? And what is it that I suggested?

If you mean I suggested rationals, then... that's CS101 and not really a suggestion as such. I can see why you'd want to template rationals (to allow different integer types) but why the policies?

Also I can't quite understand what you are trying to say in your description of your problem, and much less why the policy is templated itself?

Code: Select all

rational<int, template<typename T> checks::default_value<T, 22>  > x; 
That won't work in a nice way. What you are trying to define here is a variable of type that is a specialization of rational on a template.

Is the idea that you have policies that are parameterized over the type of rational they are applied to? I believe one way to get what you are trying to do (without necessarily agreeing that doing so is a good idea) is to parameterize the class default_value, and the type separately?

You could parameterize default_value for the integer only, and then parameterize it's methods for the type, and have the policy be perfectly normal typename template parameter?

Code: Select all

template <int v>
class default_value {
  template <class T>
  static T getvalue() {
    return (T) v;
  }  
};

template <class int_t, class policy>
class rational {
   ...
      policy::getvalue();
   ...
};

rational<int, default_value<22> > x;
Why doesn't that work?

Alternative method would be to define policies as classes with an inner class parameterized over the type, in which case your rational can create such an inner class in a given policy class to the get the specializations.

Maybe there's other possibilities. I've forgotten a lot about templates since it's been a while since I did any non-trivial hackery with them.. plus the specs (especially as implemented) keep changing...

Posted: Mon Apr 30, 2007 10:55 pm
by Candy
mystran wrote:What exactly are you trying to do? And what is it that I suggested?

If you mean I suggested rationals, then... that's CS101 and not really a suggestion as such. I can see why you'd want to template rationals (to allow different integer types) but why the policies?
I wanted the handling of checking the value before converting it to a user-specified type to be user-specified as well.
Also I can't quite understand what you are trying to say in your description of your problem, and much less why the policy is templated itself?
The policy is templated on the type in order to make it bind statically to the rest of the class so that uses of it would be optimized away if it wouldn't apply in any case. Most of the policy could trivially be removed from it to an external check but I figured, why not...
That won't work in a nice way. What you are trying to define here is a variable of type that is a specialization of rational on a template.
I was trying to tell the compiler to partially specialize the template so that it would only be templated on one parameter, and to pass that type to the template as template template parameter (so it would match and work). Seems I can only pass a full specialization of a class as type, which is a bit of a shortcoming.
Is the idea that you have policies that are parameterized over the type of rational they are applied to? I believe one way to get what you are trying to do (without necessarily agreeing that doing so is a good idea) is to parameterize the class default_value, and the type separately?

You could parameterize default_value for the integer only, and then parameterize it's methods for the type, and have the policy be perfectly normal typename template parameter?

Code: Select all

template <int v>
class default_value {
  template <class T>
  static T getvalue() {
    return (T) v;
  }  
};

template <class int_t, class policy>
class rational {
   ...
      policy::getvalue();
   ...
};

rational<int, default_value<22> > x;
Why doesn't that work?
Well... it would.
Alternative method would be to define policies as classes with an inner class parameterized over the type, in which case your rational can create such an inner class in a given policy class to the get the specializations.

Maybe there's other possibilities. I've forgotten a lot about templates since it's been a while since I did any non-trivial hackery with them.. plus the specs (especially as implemented) keep changing...
If you've been using them for a longer time, I can imagine. I've started using them only in gcc 3+ and only started serious use in 4+, which are fairly stable up to now.

Posted: Mon Apr 30, 2007 11:35 pm
by Candy
mystran wrote:If you mean I suggested rationals, then... that's CS101 and not really a suggestion as such.
Thanks.