Re: Function Overload for tr1::tuple<T>, How To?
Pete Becker wrote:
Terry G wrote:
I wish I could overload Log, so the tuples of tuples would recursively
work.
I'm so close -- but maybe like Deadhorse Point Utah State Park.
The basic problem is that you can't partially specialize template
functions.
I have it on good authority that, while this statement is correct, it
doesn't apply here. As you say, you need to overload the function
template, as Greg Herlihy suggested. But you can't provide just one
overload, unless you know that that's the exact number of template
arguments you're going to use. So here's a simpler rewrite of my example:
#include <tuple>
#include <iostream>
#include <ostream>
using std::tr1::tuple; using std::tr1::get; using std::tr1::make_tuple;
using std::cout;
template <class Ty> void print(const Ty& t)
{
cout << t;
}
void print(const tuple<>&)
{
cout << "()\n";
}
template <class A0> void print(const tuple<A0>& t)
{
cout << '(';
print(get<0>(t));
cout << ')';
}
template <class A0, class A1> void print(const tuple<A0, A1>& t)
{
cout << '(';
print(get<0>(t));
cout << ", ";
print(get<1>(t));
cout << ')';
}
template <class Ty>
void Log(const Ty& t)
{
print(t);
cout << '\n';
}
int main()
{
const char *hello = "hello";
Log(make_tuple(3, hello));
Log(make_tuple(4, make_tuple(4.5, hello)));
return 0;
}
Same output as the previous version.
--
-- Pete
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]