template <int N>
class myarray {
typedef int Bitmap;
public:
static Bitmap data[N];
};
template <int N> myarray<N>::Bitmap myarray<N>::data[N];
error: expected constructor, destructor, or type conversion before ‘myarray’
A template is a form, mold or pattern used as a guide to make something. Here are some examples of templates: Website design. Creating a document.
If you want to program in C++ there are lots of times when templates are absolutely the best option the language gives you for writing generic, reusable code. On the other hand, just because templates are the best C++ has to offer doesn't mean they're good.
Templates are fully evaluated by the compiler, and so they have zero overhead at runtime. Calling Foo<int>() is exactly as efficient as calling FooInt() . So compared to approaches which rely on more work being done at runtime, for example by calling virtual functions, templates can indeed be faster.
Since template instantiation happens at compile time, there is no run-time cost to using templates (as a matter of fact templates are sometimes used to perform certain computations at compile-time to make the program run faster).
You need typename
before myarray<N>::Bitmap
because it is a dependent type:
template <int N>
class myarray {
typedef int Bitmap;
public:
static Bitmap data[N];
};
template <int N>
typename myarray<N>::Bitmap myarray<N>::data[N];
// ^^^^^^^^
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