Skip to main content
gamedev.net
🔒 Locked

Template headache

Started by Mayrel Jul 2, 2004 at 7:46 PM 3 replies 450+ views
Original Post
Mayrel
Mayrel
I have a struct as such:

template<typename C>
struct gen_element
{
  template<typename T> gen_element (T C::*v) {}
  operator VertexArray::Format::Element () {
    return element;
  }
  VertexArray::Format::Element element;
};
I can't figure out how to specialise the constructor, I'm not even sure that it's possible. The best I could think of was

template<typename C> gen_element<C> ::
template<> gen_element<float> (float C::*v)
{
  return VertexArray::Format::Element(VertexArray::Format::FLOAT, 1, (int) v);
}
but that doesn't work. What I I want to do is

template<typename C, typename T>
VertexArray::Format::Element gen_element (T C::*v);

template<typename C>
VertexArray::Format::Element gen_element<float> (float C::*v)
{
  return VertexArray::Format::Element(VertexArray::Format::FLOAT, 1, (int) v);
}
but my compiler doesn't like partial specialisations of functions. It is right to complain?
Houdini
Houdini
I don't believe C++ supports partial specialization of functions.


- Houdini
Houdini
fallenang3l
fallenang3l
A template constructor is never a real constructor. What you're trying to do can't be done, AFAIK.
Desert Fox
Desert Fox
You cannot partially specialize a function, only full specialization is supported.
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery?" - Patrick Henry
SiCrane
SiCrane
I would just do something like:
template<typename C>struct gen_element {  template<typename T> gen_element (T C::*v);  gen_element(float C::*v);};template <typename C>template <typename T>gen_element<C>::gen_element(T C::*v) {  std::cout << "generic constructor" << std::endl;}template <typename C>gen_element<C>::gen_element(float C::*v) {  std::cout << "specific constructor" << std::endl;}struct SomeClass {  int i;  float f;};int main(int, char **) {  gen_element<SomeClass> gen1(&SomeClass::f);  gen_element<SomeClass> gen2(&SomeClass::i);  return 0;}

It's an overload rather than a partial template specialization, but it looks like it should do what you want it to.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.