simulate C++ function template instantiation with implicit conversion
I already asked [two][1] [questions][2] related to what I'm trying to do (one resolved, one of which I will close soon). I know that C++ template instantiation does not allow any implicit conversions (see for example [this comment][3]), but I would like to simulate it.

Suppose I have the following skeleton code:

    template <class T>
    struct Base_A{
        virtual void interface_func() const = 0;
    };
    template <class T>
    struct Derived_A : public Base_A<T>{
        typedef T value_type;
        void interface_func() const{}
    };

    template <class T>
    struct Base_B{
    	virtual void interface_func() const = 0;
    };
    template <class T>
    struct Derived_B : public Base_B<T>{
        typedef T value_type;
        void interface_func() const{}
    };
    
    template <class BType>
    struct Adapter : public Base_A<typename BType::value_type>{
    	BType &ref_B;
    	Adapter(const BType &inst_B):ref_B(B){}
    	void interface_func() const{} // does stuff with ref_B to simulate an A
    }
    
    template <class Should_Always_Be_Base_A>
    void f(const Should_Always_Be_Base_A &arg){
        // Only Base_A can be passed in by const ref
        // Passing in a Base_B by const ref would not work.
    }

    Derived_A<int> A;
    Derived_B<int> B;
    f(A); // passes in A by const ref
    f(B); // I want to pass in Adapter<Derived_B<int> >(B)

I want the template parameter for function `f` to always be a derived class of `Base_A` or an `Adapter`. The answer to restricting the type of `arg` [can be done][2], but the implicit conversion to an Adapter cannot. Is there any way to do this? The net result is I want to be able to call `f` as `f(A)` or `f(B)`, and in both cases I need to know the actual derived type of A or B within `f` (`f` cannot just see a reference to the base class).

**Aside:**

Presently, I just have `f(A)` working, and `f(B)` actually calls an overload which performs the Adapter construction, but I have other functions which take N arguments, each of which can be A or B, so I would need 2^N overloads.

For the curious, this is in application to the matrix library I'm working on. `Base_A` represents the base matrix type, and `Base_B` represents the base matrix-view type. For operations which will modify a matrix argument, I need to pass in the matrix by non-const reference or a modifiable matrix-view by const-ref. The adapter is just a trivial matrix-to-view adapter. So for example, I currently have a function like

    Scale(const MatrixViewBase<T> &Mview, const T &scale_factor){
        // does the actual work
    }
    Scale(MatrixBase<T> &M, const T &scale_factor){
        Scale(Adapter<MatrixBase<T> >(M), scale_factor);
    }

It's tedious and error prone to make 2^N copies of all these functions just to create the needed overloads to handle both views and non-views. As is, this is not good enough since I want Scale to be able to know the full derived type of Mview, not just the base class, because I will potentially generate instances of types dependent on Mview.

  [1]: http://stackoverflow.com/questions/1741840/c-implicit-template-instantiation
  [2]: http://stackoverflow.com/questions/1746352/how-to-have-templated-function-overloads-accept-derived-classes-from-different-ba
  [3]: http://stackoverflow.com/questions/867462/c-implicit-conversions/868398#868398
simulate C++ function template instantiation with implicit conversion
===

I already asked [two][1] [questions][2] related to what I'm trying to do (one resolved, one of which I will close soon). I know that C++ template instantiation does not allow any implicit conversions (see for example [this comment][3]), but I would like to simulate it.

Suppose I have the following skeleton code:

    template <class T>
    struct Base_A{
        virtual void interface_func() const = 0;
    };
    template <class T>
    struct Derived_A : public Base_A<T>{
        typedef T value_type;
        void interface_func() const{}
    };

    template <class T>
    struct Base_B{
    	virtual void interface_func() const = 0;
    };
    template <class T>
    struct Derived_B : public Base_B<T>{
        typedef T value_type;
        void interface_func() const{}
    };
    
    template <class BType>
    struct Adapter : public Base_A<typename BType::value_type>{
    	BType &ref_B;
    	Adapter(const BType &inst_B):ref_B(B){}
    	void interface_func() const{} // does stuff with ref_B to simulate an A
    }
    
    template <class Should_Always_Be_Base_A>
    void f(const Should_Always_Be_Base_A &arg){
        // Only Base_A can be passed in by const ref
        // Passing in a Base_B by const ref would not work.
    }

    Derived_A<int> A;
    Derived_B<int> B;
    f(A); // passes in A by const ref
    f(B); // I want to pass in Adapter<Derived_B<int> >(B)

I want the template parameter for function `f` to always be a derived class of `Base_A` or an `Adapter`. The answer to restricting the type of `arg` [can be done][2], but the implicit conversion to an Adapter cannot. Is there any way to do this? The net result is I want to be able to call `f` as `f(A)` or `f(B)`, and in both cases I need to know the actual derived type of A or B within `f` (`f` cannot just see a reference to the base class).

**Aside:**

Presently, I just have `f(A)` working, and `f(B)` actually calls an overload which performs the Adapter construction, but I have other functions which take N arguments, each of which can be A or B, so I would need 2^N overloads.

For the curious, this is in application to the matrix library I'm working on. `Base_A` represents the base matrix type, and `Base_B` represents the base matrix-view type. For operations which will modify a matrix argument, I need to pass in the matrix by non-const reference or a modifiable matrix-view by const-ref. The adapter is just a trivial matrix-to-view adapter. So for example, I currently have a function like

    Scale(const MatrixViewBase<T> &Mview, const T &scale_factor){
        // does the actual work
    }
    Scale(MatrixBase<T> &M, const T &scale_factor){
        Scale(Adapter<MatrixBase<T> >(M), scale_factor);
    }

It's tedious and error prone to make 2^N copies of all these functions just to create the needed overloads to handle both views and non-views. As is, this is not good enough since I want Scale to be able to know the full derived type of Mview, not just the base class, because I will potentially generate instances of types dependent on Mview.

  [1]: http://stackoverflow.com/questions/1741840/c-implicit-template-instantiation
  [2]: http://stackoverflow.com/questions/1746352/how-to-have-templated-function-overloads-accept-derived-classes-from-different-ba
  [3]: http://stackoverflow.com/questions/867462/c-implicit-conversions/868398#868398