M
Mark P
A couple template related questions:
1. In the code pasted below, why does the basic version get selected in
the call to foo and the const version get selected in the call to bar?
2. When working with STL output iterators, is there any general advice
on whether they should be passed by value or reference? I have
templated code which takes ostream_iterators or back_insert_iterators
which I inititally chose to pass by reference, but I've run into the
situation analogous to below where I have to duplicate my code to handle
const and non-const references to each iterator type (4 basically
identical implementations there).
3. Related to this, for my code which can take either an
ostream_iterator or a back_insert_iterator, is there any way to tell the
compiler that these two should be treated identically. Each is a model
of Output Iterator, but I can't find any way to express this in the
language. (Note, the code may also take a Back Insertion Sequence so I
can't simply have the template code assume it's argument is an Output
Iterator.)
Regarding 3, I think may best approach may be to use different function
names for the Output Iterator version and the Back Insertion Sequence
version, and let the template assume one of these models.
Thanks for your advice,
Mark
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void foo (T& t) {cout << "basic version\n";}
template <typename T>
void foo (const vector<T>& t) {cout << "const version\n";}
template <typename T>
void bar (T t) {cout << "basic version\n";}
template <typename T>
void bar (const vector<T>& t) {cout << "const version\n";}
int main ()
{
vector<int> vi;
foo(vi); // output: basic version
bar(vi); // output: const version
}
1. In the code pasted below, why does the basic version get selected in
the call to foo and the const version get selected in the call to bar?
2. When working with STL output iterators, is there any general advice
on whether they should be passed by value or reference? I have
templated code which takes ostream_iterators or back_insert_iterators
which I inititally chose to pass by reference, but I've run into the
situation analogous to below where I have to duplicate my code to handle
const and non-const references to each iterator type (4 basically
identical implementations there).
3. Related to this, for my code which can take either an
ostream_iterator or a back_insert_iterator, is there any way to tell the
compiler that these two should be treated identically. Each is a model
of Output Iterator, but I can't find any way to express this in the
language. (Note, the code may also take a Back Insertion Sequence so I
can't simply have the template code assume it's argument is an Output
Iterator.)
Regarding 3, I think may best approach may be to use different function
names for the Output Iterator version and the Back Insertion Sequence
version, and let the template assume one of these models.
Thanks for your advice,
Mark
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void foo (T& t) {cout << "basic version\n";}
template <typename T>
void foo (const vector<T>& t) {cout << "const version\n";}
template <typename T>
void bar (T t) {cout << "basic version\n";}
template <typename T>
void bar (const vector<T>& t) {cout << "const version\n";}
int main ()
{
vector<int> vi;
foo(vi); // output: basic version
bar(vi); // output: const version
}