0

I try to make a template to only work with double and a user-defined type. The user can choose the user defined type by a typedef in a included header file.

typedef float Usertype;

I solved this by writing the following in the end of my template source files:

template class SpaceDiscretizer<Datatype::Usertype>;
template class SpaceDiscretizer<double>;

However I run into a problem when the user defines Usertype as double! I tried to follow some directions, namely these topics: How can I provide template specializations for typedefs of the same type?, Conditional explicit template instantiation

struct dummy{};
template class SpaceDiscretizer< std::conditional<std::is_same<double,Datatype::Usertype>::value, dummy, double>::type>;
//typedef typename std::enable_if<false,double>::type mytype;
//template class SpaceDiscretizer<mytype>;
template class SpaceDiscretizer<Datatype::Usertype>;

The first one doesn't work,because if SpaceDiscretizer gets initialized with anything non-complicated,except for double, it does not work. But I want to give the user a easy way to add a working alternative to double in the feature. The second (commented out) try fails, because this is not an overload and if type is non existent, mytype won't be ignored, but an error is put out instead.

Edit: I have a lot of places, where I don't use a template or must ensure that multiple unconnected classes use the same datatype. I am requiered to keep the implementation in .cpp file. To solve this, I include a header file to all the relevant places, so that the user can change the value at one place and have it all be consistent. A problem arises, because I always want to initialize for double, but it would be natural for the user to specify double as the Usertype.

Community
  • 1
  • 1
AY Wer
  • 19
  • 3
  • 1
    Something about this design strikes me as a bit unusual. What's stopping someone from doing something like `using UserType = int` and then explicitly writing `SpaceDiscretizer`? That's not something you'd want to allow, but it's perfectly legal in this case. I suspect that you might be trying to do something the wrong way. – templatetypedef Jul 22 '16 at 22:49
  • 1
    not enough information in the question. however it seems as if SpaceDiscretizer<> is depending on some member of Datatype::Usertype, which is not present in a double. – Richard Hodges Jul 22 '16 at 23:00
  • @templatetypedef The idea is, that I consider the header file to be in my own or a educated developers realm, and the pure use of SpaceDiscretizer as an avoidable "end-user" error. – AY Wer Jul 23 '16 at 18:55

1 Answers1

1

I'm not sure to understand what do you want but... hoping the following example could help

#include <iostream>
#include <type_traits>

typedef  float  userT;
//typedef  double  userT;

template <typename T, bool B = std::is_same<T, double>::value>
class SpaceDiscretizer;

template <>
class SpaceDiscretizer<userT, false>
 { public: static constexpr int val = 1; };

template <>
class SpaceDiscretizer<double>
 { public: static constexpr int val = 2; };

int main ()
 {
   SpaceDiscretizer<userT>   sd1;
   SpaceDiscretizer<double>  sd2;

   std::cout << "sd1 val = " << sd1.val << std::endl;
   std::cout << "sd2 val = " << sd2.val << std::endl;

   return 0;
 }

The output is

sd1 val = 1
sd2 val = 2

when userT is defined as float and

sd1 val = 2
sd2 val = 2

when userT is defined as double.

p.s.: sorry for my bad English.

max66
  • 65,235
  • 10
  • 71
  • 111
  • Thanks for your comment, I just want to skip the explicit initialization. As far as I understand your solution, I have to specialize the class twice with the exact same intern code. I was unable to do the same with just template<> class SpaceDiscretizer; – AY Wer Jul 23 '16 at 18:03
  • @AYWer - sorry but I don't understand what do you exactly need (can be a problem with my English). What "explicit initialization" do you want to skip, exactly? And I don't understand what do you mean with "exact intern code". Take in count that mine is just an example but you can modify it in a lot of different modes. – max66 Jul 23 '16 at 18:32