1

I need a template class, which has different members, depending on which ctor is called. I managed to get a class, which has different members using sfinae with a base class (I did it almost like this SFINAE on member variable). Now my question is, can I achieve a single template class, which has different members, depending on which ctor of the class is called? Maybe someone can has an idea how to achieve this.

EDIT: I currently use boost::variant, but the problem is, that the largest object in the variant is huge, and the the smallest is ust a pointer. this is a real performance problem, because most of the time the pointer will be in the variant.

EDIT II: If this would work with a ctor it would be awesome, but if not, a factory-fuction would work as well.

EDIT III (or what I am trying to achieve): I am currently making a DSL, which translates to C++. Since I am trying to make polymorphism possible, I am only passing pointers to functions. Beacause some pointers are reference counted and some pointers are raw, depending on what the user wants, there can be shared_pointers and raw pointers of the same class. Thats why I can't make two different classes, because if a function is called on a pointer, it should be the same function, otherwise I have to overload all the fnctions, which would give me 2**n functions when the function has n arguments. Thats why I am trying to create a class, which could eigther represents a raw pointer or a shared_ptr, based on what is passed to the ctor.

Community
  • 1
  • 1
Exagon
  • 4,798
  • 6
  • 25
  • 53
  • 2
    Does it _have_ to be a constructor? Can it be a factory-function instead? – ildjarn Aug 16 '16 at 13:04
  • 3
    This looks like a weird idea... What are you trying to do exactly? – Holt Aug 16 '16 at 13:05
  • @ildjarn: Exactly my idea :-) Create a generator/factory which delivers objects on demand. – Klaus Aug 16 '16 at 13:06
  • a factory function would be quite ok – Exagon Aug 16 '16 at 13:07
  • 1
    if a factory method is ok, simply use it :-) – Klaus Aug 16 '16 at 13:18
  • @Exagon Now, I'm coming from an OOP perspective (which I generally try to avoid in c++), but would a class hierarchy solve anything? Or maybe just separate classes that have common public members that can be used in a template? Either way, it looks like you could benefit with separate types. – PC Luddite Aug 16 '16 at 13:20
  • the problem is that the "both types" have to be passed by copy to a lot of functions, so using oop is difficult because there of the passing by value i cant use polymorphism – Exagon Aug 16 '16 at 13:24
  • Could you give an example of what you're trying to achieve? Maybe we can help you rework it. – PC Luddite Aug 16 '16 at 13:51
  • I eddited the question, trying to explain what I try to achieve – Exagon Aug 16 '16 at 14:10
  • 2
    @Exagon If you only manage raw pointer and `shared_ptr`, I don't see what is "huge" in your `boost::variant`? – Holt Aug 16 '16 at 14:25
  • I did not understand what you want! "because if a function is called on a pointer, it should be the same function,"??? If you use a template, you have definitely 2 functions build from one template. If you want to use the "same" function, you have to convert the pointer before, which is easy, if the function did not "eat" the pointer and delete the underlying object. Where is your pointer used a) ptr->Func() or b) Func( ptr)??? It is still unclear! – Klaus Aug 16 '16 at 14:27

1 Answers1

1

You should simply continue using variant<> but instead of storing your huge class as an object, store it as a pointer as well:

boost::variant<common_case*, huge_class*>

Since you say you usually store a pointer anyway, this doesn't cost you anything, and reclaims 100% of the wasted memory because all object pointers are the same size.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436