I'm having trouble declaring a template class. I've tried an number of ill-readable and non-sensical combinations.
template <class C, class M >
class BlockCipherGenerator : public KeyGenerator
{
...
private:
M < C > m_cipher;
};
And
template <class C, class M >
class BlockCipherGenerator : public KeyGenerator
{
typedef typename C::value_type CIPHER;
typedef typename M::value_type MODE;
private:
MODE < CIPHER > m_cipher;
};
A template is a simple yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types.
To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>(float original); Template arguments may be omitted when the compiler can infer them.
Class Template DeclarationA class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. template <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... };
As long as you are satisfied with automatic type inference, you can use a template constructor (of a non-template class). @updogliu: Absolutely. But, the question is asking about "a template constructor with no arguments" If there are no function arguments, no template arguments may be deduced.
It's what it says.
Your template parameter list says that M
is a class
, not a template
.
If you say that it's a class template, then everything's fine:
template <class C, template <class C> class M>
class BlockCipherGenerator : public KeyGenerator
{
M<C> m_cipher;
};
Remember, something like std::vector
is not a class, but a class template. Something like std::vector<int>
is a class (type).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With