4

I'd like to use the inferred type from the constructor argument as template arguments throughout the rest of my class. Is this possible?

Something like this:

    class AnyClass 
    {
    public:
      template<Class C>
      AnyClass(C *c) {
      //class C is inferred by constructor argument
      }
      // constructor argument is used as argument in other template types
      nestclass<C> myNestedClass;
      void randomfunction(C *randonarg) {
      }
    }

Details:

Here's the thing. I'm trying to initialize my base type given the type of the inheriting class. In the case below DerivedA inherits from Base, but DerivedB inherits from DerivedA, therefore from what I understand the value of this in the constructor of Base (found in DerivedA) will actually be a pointer to DerivedB therefore the inferred type in Base will be of type DerivedB. However I'd like to use this type throughout the rest of my Base class and not just limit it to the constructor.

class Base {

// type T derived from inheriting class
template<T>
Base(T *) {};
//like to use other places
void randomfunction(T *arg1) {
//does something with type T
};
}

class DerivedA : publicBase {

DerivedA() : Base(this) { //this should be a pointer to DerivedB, since it is inherited 
                          //from DerivedB.
}

}

class DerivedB : class DerivedA {
//anything
}

**My main goal is to use the inheriting class type in my base class. I realize this is sort of a different qst, but I was thinking my solution would be found somehow in my original question.

I'm thinking using a go-between method (similar to the function proposed below) but not sure if it will work.

Thanks for the help!

Bob Blogge
  • 186
  • 9
  • No. I gave an answer to a related question below, but you could describe your use case in more detail and mayhap the real problem you have can be solved! – Yakk - Adam Nevraumont Apr 07 '13 at 16:32
  • 1
    Maybe you want to take a look at [CRTP](http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern) (curiously recurring template pattern)? – dyp Apr 07 '13 at 17:02
  • Yes, the CRTP is the right way to do this. If you need a single binary interface to the parent, do a two-stage CRTP, with an interface class and a CRTP implementation base and then a derived. – Yakk - Adam Nevraumont Apr 07 '13 at 18:59

1 Answers1

5

No.

But you can make your entire class a template class, and create a factory function that does type deduction to generate your template class.

template<typename C>
class AnyClass {
public:
  AnyClass( C* c );
  std::vector<C> vec;
};
AnyClass<C> make_any_class( C* c ) {
  return AnyClass<C>(c);
};
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • I understand this, but I'm afraid it defeats the original purpose of `AnyClass`. Judging from the name, I would tend to believe the OP wanted to achieve the kind of type erasure that `boost::any` offers – Andy Prowl Apr 07 '13 at 16:05
  • @AndyProwl possibly -- I presumed the thing that OP wanted was the ability to construct their class from an inferred type, rather than a uniform class with multiple types within it. And I'd suspect that the kind of type erasure that `boost::function` offers would be more suited: where you use the `pImpl` pattern store a type-erased `AnyClass_impl*` and forward methods from your `AnyClass` to your `AnyClass_impl_interface*`. But really, the answer to the OP's question is "No", and in order to pass the character limit I included an answer to a question the OP didn't ask. – Yakk - Adam Nevraumont Apr 07 '13 at 16:31
  • Yes, I understand what you mean (probably something similar to [this](http://stackoverflow.com/a/15175744/1932150)). But it is true that the direct answer to the OP's specific question is "No", so +1 – Andy Prowl Apr 07 '13 at 16:41
  • Sorry for the delay. I added details to my question. Thanks. – Bob Blogge Apr 07 '13 at 16:58