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 <boost/utility/enable_if.hpp> using namespace std; using namespace boost; template<typename T> struct A {}; struct B : public A<int> {}; template<typename T> struct isA { static const bool value = false; }; template<typename T> struct isA< A<T> > { static const bool value = true; }; // This would give the desired result, but I don't know how it should // legally be expressed it in C++ (if anyhow): //template<typename T, typename TEnabler = void> //struct A { static const bool value = false; }; // //template<typename T, > //struct isA< T, // enable_if<{{T where T is convertible to A<T1> for some T1}}> > //{ // static const bool value = true; //}; int _tmain(int argc, _TCHAR* argv[]) { cout << isA< A<int> >::value << endl; // 1 cout << isA< A<double> >::value << endl;// 1 cout << isA< B >::value << endl; // 0 - not what I want cout << isA<int>::value << endl; // 0 return 0; } ---------------------------------------------------
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<from, to>. John.
"John Maddock" <john@johnmaddock.co.uk> az alábbiakat írta a következo hírüzenetben: 015301c4f4a8$924a1c30$724d0352@fuji...
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<from, to>.
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<from, to>.
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<from, A<_> > -- Aleksey Gurtovoy MetaCommunications Engineering
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<from, to>.
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<from, A<_> >
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