Logo MTL4

Namespace qualification

All classes and functions are defined in namespace mtl or sub-namespaces thereof. Matrix-related functions and types are defined in mtl::matrix and vector material likewise in mtl::vector. To make applications shorter, often used types like compressed2D and dense_vector are imported into the namespace mtl. As a consequence, you can write mtl::compressed2D<..> or mtl::matrix::compressed2D<..>.

Functions are defined as much as possible in the namespaces mtl::matrix and mtl::vector. Therefore, Argument-Dependent Lookup (ADL) finds these functions without namespace qualification. For instance, if we call trans(x) the mtl::matrix::trans() is called if x is a matrix, i.e. the type of x is defined in mtl::matrix. Likewise if x is a vector. If the type of x is not defined in MTL4, you must qualify the function because ADL does not apply. Alternatively, you can import your type into the appropriate namespace:

namespace mtl { namespace matrix {
    using my_namespace::my_matrix_type;
}}
In both cases you would also need a handful of type traits (the documentation of integrating external types into MTL4 is pending).

To avoid all namespace qualifications, you can use

using namespace mtl;
in your program before you use the first MTL4 item. For small programs this is probably the easiest solution.

If you work at a larger project, it is better not importing the entire namespace. Instead you have to qualify all types and NOT qualify the functions. If you call mtl::trans it will not find it because it is defined in sub-namespaces. In general, you should avoid explicit qualification as mtl::f(...).

Functions with a special behavior are those which are defined upon MTL4 types and non-MTL4 types at the same type. An example is size(). Like trans it is defined in mtl::matrix and mtl::vector. It also works for std::vector and for arrays. The definitions of these functions are in namespace mtl (we did not like defining it in std:: or the global namespace). The best way using such functions is

using mtl::size;
unsigned n= size(x);
This works for all supported types of x. If x is a matrix then mtl::matrix::size is called and if x is a std::vector mtl::size is called (which is implemented with partially specialized functor but this is another topic).

As a rule of thumb. If you call an unqualified function for an MTL4 type ADL will find it (otherwise it is sloppily implemented and must be fixed). If you call an MTL4 function for a non-MTL4 type write using mtl::f before calling f. For generic functions that handle both MTL4 and non-MTL4 types also write using mtl::f. If it still not compile, the function is probably not implemented yet.

Return to Why and How we use Functors                                Table of Content                                Proceed to Copying in MTL4


Namespace qualification -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine -- Gen. with rev. 7542 on 7 Apr 2011 by doxygen 1.5.9 -- © 2010 by SimuNova UG.