Ok, I'm trawling through the tutorials, and have got to templates, the syntax of which is a little baffling, although the concept reasonably straightforward. I had this test question below, can you tell me why the right answer is the right answer! TIA.
Which of the following is an invalid template declaration:
a) template <int x> int func() {return x;}
b) template <double x> double func() {return x;}
c) template <typename x> void func(x t) {}
I honestly don't know the answer to this one. =x If I had to guess I'd say B, only because double is a nonintegral type and maybe C++ templates don't like that. But I don't know for sure.
On a somewhat related note: I never understood the point of using variables as template parameters (like a or b in your example -- what's the point?). I mean why not just pass them as a function/ctor parameter. Does anyone know why they'd be benefitial?
From C++ Templates - the complete guide book4.3 Restrictions for Nontype Template Parameters
Note that nontype template parameters carry some restrictions.
In general, they may be constant integral values (including enumerations) or
pointers to objects with external linkage.
Floating-point numbers and class-type objects are not allowed as nontype template parameters:
template <double VAT> // ERROR: floating-point values are not
double process (double v) // allowed as template parameters
{
return v * VAT;
}
template <std::string name> // ERROR: class-type objects are not
class MyClass { // allowed as template parameters
…
};
Not being able to use floating-point literals (and simple constant floating-point expressions)
as template arguments has historical reasons. Because there are no serious technical
challenges, this may be supported in future versions of C++ (see Section 13.4 on page 210).
It is arguably simpler to understand how the sizeof method works, but it is not simpler to use, and it is definitely much more prone to misuse. Example:
1 2 3 4 5 6 7 8 9
int main() {
int* parray = newint[ 10 ];
// Compiles, but gives wrong answer (runtime bug)
std::cout << "Size of array is " << (sizeof(parray)/sizeof(parray[0]));
// Does not compile (compile time bug)
std::cout << "Size of array is " << ArraySize( parray );
}
I'd rather have compile time bugs than runtime bugs.
From C++ Templates - the complete guide book
4.3 Restrictions for Nontype Template Parameters
Note that nontype template parameters carry some restrictions.
In general, they may be constant integral values (including enumerations) or
pointers to objects with external linkage.
Floating-point numbers and class-type objects are not allowed as nontype template parameters:
template <double VAT> // ERROR: floating-point values are not
double process (double v) // allowed as template parameters
{
return v * VAT;
}
template <std::string name> // ERROR: class-type objects are not
class MyClass { // allowed as template parameters
…
};
Not being able to use floating-point literals (and simple constant floating-point expressions)
as template arguments has historical reasons. Because there are no serious technical
challenges, this may be supported in future versions of C++ (see Section 13.4 on page 210).
template <double VAT>
I don't understand why this is a template? As it is specifying the type....... v confused.
I don't understand why this is a template? As it is specifying the type....... v confused.
The 'template' keyword is a dead givaway =P
</wiseass>
Template declarations are the same as non-template declarations, only they're prefixed by the template keyword (and any template arguments).
IE:
1 2 3 4 5 6
// a non-template function
void foo(int bar) { }
// a template function
template <typename T>
void foo(T bar) { }
Syntax for each is exactly the same, but the template keyword slips in before the rest (even before the return type). That is what makes it a template.
But again note that you can't use double as a template parameter.
template <int x> int func() {return x;} This is apparently a template, why? template <double x> double func() {return x;} this is not even legal, why? apart from undefined historical reasons.
Now I can understand why this :-
template <typename T> is a template as we haven't defined the type
But the two at the top, why bother with the word template??
1) func returns and int and takes no parameters, what else can it do? what flexibility does the template <int x> do ?
2) Not even legal, but why oh why, just dump the template <double x> specifier.