B
bartek
Please consider the following scenario below (sketch of).
There are two templates defined: A and B, both with mutual conversion
operators defined.
Also, there's a free function template 'do_something' with the following
signature:
template <class T>
inline void do_something(B<T> const& arg);
Now, given an object of type, say, A<int> passed as a parameter to that
function, the compiler gives up when deducing template arguments. Is it
possible to somehow go around this, without using explicit template
specification? It's a show-stopper for templated operators.
One possible solution I can think of is, of course, to derive both A and
B from some common base class, and use the 'curious base' pattern...
I'd like to keep it simple, though...
See below for the whole picture.
//-------------------------------------------------------------------
// forward declarations
template <class T> class A;
template <class T> class B;
//-------------------------------------------------------------------
template <class T>
class A {
T member;
public:
A(T const& from) : member(from) { }
operator B<T>() const { return member; }
};
//-------------------------------------------------------------------
template <class T>
class B {
T member;
public:
B(T const& from) : member(from) { }
operator A<T>() const { return member; }
};
//-------------------------------------------------------------------
template <class T>
inline void do_something(B<T> const& arg) {
// do something
}
//-------------------------------------------------------------------
int main() {
A<int> a(5);
do_something(a); // ERROR - could not deduce template argument.
do_something<int>(a); // OK
return 0;
}
There are two templates defined: A and B, both with mutual conversion
operators defined.
Also, there's a free function template 'do_something' with the following
signature:
template <class T>
inline void do_something(B<T> const& arg);
Now, given an object of type, say, A<int> passed as a parameter to that
function, the compiler gives up when deducing template arguments. Is it
possible to somehow go around this, without using explicit template
specification? It's a show-stopper for templated operators.
One possible solution I can think of is, of course, to derive both A and
B from some common base class, and use the 'curious base' pattern...
I'd like to keep it simple, though...
See below for the whole picture.
//-------------------------------------------------------------------
// forward declarations
template <class T> class A;
template <class T> class B;
//-------------------------------------------------------------------
template <class T>
class A {
T member;
public:
A(T const& from) : member(from) { }
operator B<T>() const { return member; }
};
//-------------------------------------------------------------------
template <class T>
class B {
T member;
public:
B(T const& from) : member(from) { }
operator A<T>() const { return member; }
};
//-------------------------------------------------------------------
template <class T>
inline void do_something(B<T> const& arg) {
// do something
}
//-------------------------------------------------------------------
int main() {
A<int> a(5);
do_something(a); // ERROR - could not deduce template argument.
do_something<int>(a); // OK
return 0;
}