Re: Is this legal C++?
On 10 Jun., 03:59, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Jun 8, 6:21 am, arrowtackett <arrowtack...@gmail.com> wrote:
Below is a small piece of code that I'm curious about. It is a
recursive template function that accepts a vector of any dimension.
The function returns the dimension of the vector passed into it. It
seems that the typename parameter "A" is allowed to be a template
itself. This code doesn't give me any warnings, but Is this legit C+
+? I had never seen this before, and I have just recently discovered
this by accident. If this is indeed legal C++, this is a very nice
way for a function to accept N-dimensional vectors.
// file.cpp
#include <iostream>
#include <vector>
using namespace std;
template<typename A> int getDimension(const vector<A> &, int = 0);
template<typename A> int getDimension(const A &, int);
template<typename A>
int getDimension(const vector<A> &vec, int dims = 0) {
++dims;
int totalDims = getDimension(vec[0], dims);
return totalDims;
}
// Overloaded function that is called when the typename
// parameter is not a vector (the last recursive iteration)
template<typename A>
int getDimension(const A &vec, int dims) {
return dims;
}
int main() {
typedef vector<double> V1;
typedef vector<vector<double> > V2;
typedef vector<vector<vector<double> > > V3;
V3 vec3D(2, V2(3, V1(4, 0.0)));
int dim = getDimension(vec3D);
cout << "The dimension of the vector is: " << dim << endl;
return 0;
}
While it works, it is not quite correct. The reason being that
std::vector<> has two template arguments. Some compilers allow to omit
the template arguments of a template for which there is a default
value (std::vector<> has a default value for its second template
argument). So, to be pedantically precise and portable getDimension
signature should be: [... cut ...]
I'm sorry but you are wrong. The std::vector template does not have
any arguments (default arguments, at most), but it has at least two
parameters. I've seen some people saying the same, so i thought about
stating the infamous You-Are-Wrong message :)
The very definition of a default argument is that it can be omitted.
The purpose of vector<T> in the function template function-parameter
list is to provide a type P that is a class template specialization.
It's compared to an argument type A, which is a class template
specialization too, to find matching values of T, which makes type P
identical to type A. Its purpose is not to specify the formal
*parameter-list* of a template, but its purpose is to specify the
actual *argument-list* supplied to the parameters of a class template
(vector in this case), to name a specialization of that template.
And by definition, default arguments may be omitted. And in any way,
if it were as you say, you should have to worry about any additional
optional argument that may be accepted by an implementation. This
stuff only is a concern when dealing with template-template
parameters, in which case you need to specify the formal *parameter-
list* of the template template parameter in question.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]