Hello,
how do I express through template metaprogramming or in a similar way that a
class is convertible to a template
class for some T1?
I.e., the condition: "There exists some T1 such that B is convertible to
A<T1>".
An example program: (Platform: VC++7.1)
---------------------------------------------------
#include <iostream>
#include
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>". An example program: (Platform: VC++7.1)
I guess you want the type-trait: is_convertible
"John Maddock"
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>". An example program: (Platform: VC++7.1)
I guess you want the type-trait: is_convertible
.
Oh, if only it were such easy! is_convertible cannot be used with template classes, as far as I know. This is what causes the problem. (A is a template class)
John.
John Maddock writes:
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>". An example program: (Platform: VC++7.1)
I guess you want the type-trait: is_convertible
.
In his case, 'to' is an unbound family of types ('A<X>' for any 'X'),
so 'is_convertible' won't do it. May be we should consider getting
this in the library, in some form. E.g. tweak 'is_convertible' to
support something like
is_convertible
Aleksey Gurtovoy wrote:
John Maddock writes:
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>". An example program: (Platform: VC++7.1)
I guess you want the type-trait: is_convertible
. In his case, 'to' is an unbound family of types ('A<X>' for any 'X'), so 'is_convertible' won't do it. May be we should consider getting this in the library, in some form. E.g. tweak 'is_convertible' to support something like
is_convertible
>
How are you going to implement it? Overload resolution for function templates ignores user-defined conversions. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
Agoston Bejo writes:
Hello,
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>".
Use overload resolution. The easiest way would probably be to take 'is_convertible' implementation for your compiler and replace the overload used to detect 'To' type convertibility with with a function temlate accepting 'A<T1>'. HTH, -- Aleksey Gurtovoy MetaCommunications Engineering
Aleksey Gurtovoy wrote:
Agoston Bejo writes:
Hello,
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>".
Use overload resolution. The easiest way would probably be to take 'is_convertible' implementation for your compiler and replace the overload used to detect 'To' type convertibility with with a function temlate accepting 'A<T1>'.
But template argument deduction will fail, won't it, since user defined conversions aren't considered when deducing template arguments? Obviously, if the class is derived from A<T1> for some T1, then it should work, since the derived-to-base conversion is considered. Tom
Tom Widmer writes:
Aleksey Gurtovoy wrote:
Agoston Bejo writes:
Hello,
how do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>".
Use overload resolution. The easiest way would probably be to take 'is_convertible' implementation for your compiler and replace the overload used to detect 'To' type convertibility with with a function temlate accepting 'A<T1>'.
But template argument deduction will fail, won't it, since user defined conversions aren't considered when deducing template arguments?
Yes, it will.
Obviously, if the class is derived from A<T1> for some T1, then it should work, since the derived-to-base conversion is considered.
This was the case with the OP code, and what I was thinking of when I wrote the above. Thanks for the correction, -- Aleksey Gurtovoy MetaCommunications Engineering
participants (5)
-
Agoston Bejo
-
Aleksey Gurtovoy
-
David Abrahams
-
John Maddock
-
Tom Widmer