Help with template function overloading
Dear all,
I need to write a function, say "do_something", which differs in the
number and type of input (template) parameters.
Specifically, I'd like to call "do_something" in these ways:
* do_something(v) // 1 template argument
* do_something<1>(m) // 1 compile-time arg and 1 template argument
* do_something<tag>(m) // 2 template arguments
where "v" is a vector and "m" is a matrix (so they have different
types), and "tag" is a tag-like class.
At the end of this email there is a toy example which should make you
things more clear (... not so toy since I need to integrate this kind
of functions in Boost.uBLAS).
If I compile this code with GCC 4.4.4,
$ g++ -Wall -Wextra -ansi -pedantic -g -o do_something
do_something.cpp
I get this error:
--- [error] ---
do_something.cpp: In instantiation of ?vector_traits<row_major_tag>?:
do_something.cpp:101: instantiated from here
do_something.cpp:60: error: no type named ?value_type? in ?struct
row_major_tag?
--- [/error] ---
Could someone point me out where I am wrong and how to fix it?
--- [code] ---
#include <cstddef>
#include <iostream>
template <typename ExprT>
struct expr
{
typedef ExprT expr_type;
};
template <typename ExprT>
struct vector_expr: expr< vector_expr<ExprT> >
{
typedef ExprT expr_type;
expr_type const& operator()() const
{
return *static_cast<expr_type*>(this);
}
expr_type& operator()()
{
return *static_cast<expr_type*>(this);
}
};
template <typename ExprT>
struct matrix_expr: expr< matrix_expr<ExprT> >
{
typedef ExprT expr_type;
expr_type const& operator()() const
{
return *static_cast<expr_type*>(this);
}
expr_type& operator()()
{
return *static_cast<expr_type*>(this);
}
};
template <typename T>
struct vector: vector_expr< vector<T> >
{
typedef T value_type;
};
template <typename T>
struct matrix: matrix_expr< matrix<T> >
{
typedef T value_type;
};
struct row_major_tag {};
struct column_major_tag {};
template <typename V>
struct vector_traits
{
typedef typename V::value_type value_type;
};
template <typename M>
struct matrix_traits
{
typedef typename M::value_type value_type;
};
template <typename ExprT>
typename vector_traits<ExprT>::value_type
do_something(vector_expr<ExprT> const& ve)
{
std::cout << "do_something<ExprT>" << std::endl;
return 0;
}
template <std::size_t Dim, typename ExprT>
typename matrix_traits<ExprT>::value_type
do_something(matrix_expr<ExprT> const& me)
{
std::cout << "do_something<size_t,ExprT>" << std::endl;
return 0;
}
template <typename TagT, typename ExprT>
typename matrix_traits<ExprT>::value_type
do_something(matrix_expr<ExprT> const& me)
{
std::cout << "do_something<TagT,ExprT>" << std::endl;
return 0;
}
int main()
{
typedef double value_type;
vector<value_type> v;
matrix<value_type> m;
value_type res;
res = do_something(v);
res = do_something<1>(m);
res = do_something<row_major_tag>(m);
}
--- [/code] ---
Thank you very much in advance!!
Best,
-- Marco
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]