 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
korusef@gmail.com Guest
|
Posted: Sat Dec 02, 2006 10:10 am Post subject: Testing existence of template type |
|
|
Class TypeTraits tries to verify, whether its templated type contains
required types.
It works for non-template types, but I don't know, how to check for the
template one.
So is there any way to check for the existence of TestType::B?
#include <boost/utility/enable_if.hpp>
#include <boost/concept_check.hpp>
#include <iostream>
using namespace std;
template <typename T> struct TypeTautology { enum { value = true};};
template <class GP, typename Enable = void>
struct TypeTraitsImpl
{ enum { value = false }; };
template <class GP>
struct TypeTraitsImpl<
GP
,typename boost::enable_if<
TypeTautology<
typename GP::A//works ok, checks existence of A
//typename GP::B//compiles, but returns "test: 0"
//typename GP::template B//doesn't compile
//typename GP::template <class> B//doesn't compile
| Quote: |
::type
{ enum { value = true }; }; |
template <class GP> struct TypeTraits: public TypeTraitsImpl<GP, void>
{};
struct TestType
{
struct A {};
template <class PG> struct B {};
};
int main(int const, char const* [])
{
try
{
clog << "test: " << TypeTraits<TestType>::value << endl;
//Expected output:
//test: 1
return EXIT_SUCCESS;
}
catch (...){
cerr << "error" << endl;
}
exit(EXIT_FAILURE);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sun Dec 03, 2006 5:24 pm Post subject: Re: Testing existence of template type |
|
|
korusef (AT) gmail (DOT) com wrote:
| Quote: | Class TypeTraits tries to verify, whether its templated type contains
required types.
It works for non-template types, but I don't know, how to check for the
template one.
So is there any way to check for the existence of TestType::B?
#include <boost/utility/enable_if.hpp
#include <boost/concept_check.hpp
#include <iostream
using namespace std;
template <typename T> struct TypeTautology { enum { value = true};};
template <class GP, typename Enable = void
struct TypeTraitsImpl
{ enum { value = false }; };
template <class GP
struct TypeTraitsImpl
GP
,typename boost::enable_if
TypeTautology
typename GP::A//works ok, checks existence of A
//typename GP::B//compiles, but returns "test: 0"
//typename GP::template B//doesn't compile
//typename GP::template <class> B//doesn't compile
::type
{ enum { value = true }; };
|
A template is not a type, so TypeTraitsImpl (since it detects the
existence of types only) is not able to test for the existence of a
template - directly. TypeTraitsImpl is able to test for a template
indirectly, by testing for one of its instantiations (since an
instantiation which would be a type). If any one of its instantiated
types is found (it does not matter which one), then the template itself
must also exist:
template <class GP>
struct TypeTraitsImpl<
GP,
typename boost::enable_if<
TypeTautology<
typename GP::template B<int> // returns "test: 1"
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|