Designing Inheritance with Abstract Return Types
===

Here's a rough image of what I'm trying to achieve (it won't compile, so consider it pseudocode). Please note that even though the example is based on public key cryptographic schemes, the question is about design patterns, templates and inheritance.

    class CryptoProvider
    {
    public:
    	template <typename T>
    	virtual T Encrypt ()
    	{
    		T data;
    
    		return data;
    	}
    };
    
    class Paillier : public CryptoProvider
    {
    public:
    	typedef int Ciphertext;

    	Ciphertext Encrypt ()
    	{
    		Ciphertext data;
    		return data;
    	}
    };
    
    class ElGamal : public CryptoProvider
    {
    public:
    	struct Ciphertext
    	{
    	public:
    		int c1;
    		int c2;
    	};
    
    	Ciphertext Encrypt ()
    	{
    		Ciphertext data;
    		return data;
    	}
    };

Basically, I want to provide some generic functionality in CryptoProvider, which can be overridden by the derived classes if required, because otherwise I would end up with lots of duplicate code. If `Encrypt` just needs to create a variable and call another function, then I don't want to write this code in every derived class, but, if a derived class needs to do some extra processing on the data, it should be able to overwrite the base method.

The biggest limitation that I've ran into is to somehow be able to override the Encrypt function by specifying completely different return types. Could somebody point me in the right direction on how to achieve this? Should I stick to traditional inheritance or should I try to go for compile-time / static polymorphism with [CRTP][1]? I have no idea where to start.

I also want to impose the derived classes to implement certain methods, but I'm not sure how to achieve this, if the solution to the first issue requires that I discard standard inheritance (and I won't benefit from virtual methods)...


  [1]: http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern