By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Templates & automatic type conversion

P: n/a

Consider the attached example. When I try to compile it (with g++
4.1.2), I get the error message

error: no matching function for call to 'fun(A<int>&)'

However, if I do not use templates, it compiles without errors.

Could someone please explain why this happens? Is there a workaround?

------------->8---------------

// With A and B templates

template<typename T>
struct B { };

template<typename T>
struct A {
operator B<T() { return B<T>(); }
};

template<typename T>
void fun(B<Tb) { }

int main() {
A<inta;
fun(a);
return 0;
}

------------->8---------------

// Without templates

struct B { };

struct A {
operator B() { return B(); }
};

void fun(B b) { }

int main() {
A a;
fun(a);
return 0;
}
Jun 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Szabolcs wrote:
>
Consider the attached example. When I try to compile it (with g++
4.1.2), I get the error message

error: no matching function for call to 'fun(A<int>&)'

However, if I do not use templates, it compiles without errors.

Could someone please explain why this happens? Is there a workaround?

------------->8---------------

// With A and B templates

template<typename T>
struct B { };

template<typename T>
struct A {
operator B<T() { return B<T>(); }
};

template<typename T>
void fun(B<Tb) { }

int main() {
A<inta;
fun(a);
return 0;
}

------------->8---------------

// Without templates

struct B { };

struct A {
operator B() { return B(); }
};

void fun(B b) { }

int main() {
A a;
fun(a);
return 0;
}
I would like to mention that even if the conversion path is provided
through a constructor of B, like

template<typename T>
struct B { B(A<T&) {} }

implicit conversion does not work unless the function fun(B b) is not a
template. So is there no way of making implicit conversion work in
function templates?

To explain my motivation for doing this: I need to create the const and
non-const versions of a class separately, because, for certain reasons,
using the const qualifier does not work in this case. The non-const
version should be implicitly converted to the const version whenever
necessary.
Jun 10 '07 #2

P: n/a
On Jun 10, 11:21 am, Szabolcs <szhor...@gmail.comwrote:
Consider the attached example. When I try to compile it (with g++
4.1.2), I get the error message
error: no matching function for call to 'fun(A<int>&)'
However, if I do not use templates, it compiles without errors.
Could someone please explain why this happens?
------------->8---------------
// With A and B templates
template<typename T>
struct B { };
template<typename T>
struct A {
operator B<T() { return B<T>(); }
};
template<typename T>
void fun(B<Tb) { }
int main() {
A<inta;
fun(a);
Here, there is no function fun, only a function template. The
compiler procedes by trying to do type resolution on the
template. It needs a function with a signature "fun(A<int>)";
it can get by with "fun(A<int>&)", or "fun(A<intconst&)", but
that's about it. So type deduction fails, the function overload
set is empty, and overload resolution complains that it cannot
find a match.
return 0;
}
------------->8---------------
// Without templates
struct B { };
struct A {
operator B() { return B(); }
};
void fun(B b) { }
int main() {
A a;
fun(a);
Here, the compiler has a function, "fun(B)", to add to the
overload set, without any need to do type deduction or
instantiate a template. Since it is the only function in the
overload set (and it can be called), it is the unique best
match.
return 0;

}
Is there a workaround?
You've found it: don't use templates:-).

Seriously, if you want complex, user defined conversions to be
considered, you might try offering a second template:

template< typename T >
void
fun( B< T b )
{
fun( static_cast< A< T ( b ) ;
}

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #3

P: n/a
Szabolcs wrote:
>
Consider the attached example. When I try to compile it (with g++
4.1.2), I get the error message

error: no matching function for call to 'fun(A<int>&)'

However, if I do not use templates, it compiles without errors.

Could someone please explain why this happens? Is there a workaround?

------------->8---------------

// With A and B templates

template<typename T>
struct B { };

template<typename T>
struct A {
operator B<T() { return B<T>(); }
};

template<typename T>
void fun(B<Tb) { }

int main() {
A<inta;
fun(a);
return 0;
}
perhaps you can overload fun

template<typename T>
void fun(A<T& b) { }

perhaps you can be explicit

fun<int>(a)

perhaps you can open up fun for any type

template<typename T>
void fun(T & b) { }
Jun 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.