#include <iostream>
#include <vector>
using namespace std;
template< typename T > using matrix = vector< vector<T> > ;
I saw this code snippet while searching for templates. I was wondering why there's a word "using". What is it for?
Thanks :)
#include <iostream>
#include <vector>
using namespace std;
template< typename T > using matrix = vector< vector<T> > ;
I saw this code snippet while searching for templates. I was wondering why there's a word "using". What is it for?
Thanks :)
using my_type = old_type
is a new syntax in C++11 which is basically the same as using typedef old_type my_type
, with the important difference that it can be templated (whereas typedefs can't be).
So in the code you've shown, matrix<T>
is defined as an alias for vector< vector<T> >
(for all types T).
so instead of using vector< vector<T> >
I will use matrix<T> sampleArray
to identify sampleArray
as a vector? What happens if I removed the "using" word? Will it affect my program?
What happens if I removed the "using" word? Will it affect my program?
It would fail to compile...
I have this code
#include <iostream>
#include <vector>
using namespace std;
template< typename T > using matrix = vector< vector<T> >;
template< typename T >
matrix<T> getArray( const matrix<T>& array )
{
// do something
}
int main()
{
matrix <int> array = { {1,2,3,4}, {5,6,7,8}, {9,0,1,2}, {3,4,5,6}, {7,8,9,0} };
cout << matrix;
auto block = getArray( matrix, 2, 2, 1 );
cout << block;
return 0;
}
But it gives the following error:
6:24: error: expected unqualified-id before ‘using’
11:1: error: ‘matrix_t’ does not name a type
In function ‘int main()’:
40:5: error: ‘matrix_t’ was not declared in this scope
40:15: error: expected primary-expression before ‘int’
40:15: error: expected ‘;’ before ‘int’
51:1: error: expected ‘}’ at end of input
Any comments about this?
As I said, it's a new feature in C++11. It looks like either your compiler does not yet support C++11 or you didn't enable that support. If you're using g++, you need to add -std=c++11
to the command line (or enable the appropriate settings in your IDE).
Once you've fixed that, you'll still get error messages, but they won't be about your usage of using
. Specifically you're using matrix
where you meant to use array
a couple of times. Also you can't use vectors as operands to cout <<
.
OK thanks I'll try to install C++11 and see what happens
The only compiler that currently supports "template aliases" (which is the name of this feature) is GCC version 4.7.0 and later (experimental). So, that's the compiler you need to install if you want that code to work. For Windows, MinGW has recently release a version with 4.7.0. As for Linux, you can look for up-stream repositories that carry bleeding-edge GCC versions, or you can build it from source (as I do).
As for Linux, you can look for up-stream repositories that carry bleeding-edge GCC versions, or you can build it from source (as I do).
FWIW Arch Linux has gcc 4.7.1 in its main repository.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.