H
Howard Gardner
/*
If I am using boost, then how should I write this program?
As it sits, this program is using SFINAE to determine whether
or not a type supports particular syntax. I suspect that there
is functionality in boost to do this.
I have found mpl::has_xxx, which I suspect of being part of the
solution. I've also found type_traits::has_nothrow_constructor
(etc.), which I suspect of using the solution: unfortunately, those
implementations are rotten with unfamiliar macros, which makes
them hard to follow.
What I'm looking for is an approachable sample of using the
relevant boost facility.
*/
#include <fstream>
using std::cout;
using std::endl;
template< typename x, typename xSyntax >
struct supports_syntax
{
private:
typedef char ret_true;
struct ret_false{ char c[2]; };
template< typename xTested >
static
ret_false
func( ... );
template< typename xTested >
static
ret_true
func( typename xSyntax::template match< xTested > * );
public:
static
const bool
value
= ( sizeof( func< x >( 0 ) ) == sizeof( ret_true ) );
};
template< typename xParm >
struct syntax_insert
{
template< typename y, y & (y::*)( xParm ) = &y::insert >
struct match;
};
struct syntax_type
{
template< typename y, typename xType = typename y::type >
struct match;
};
struct yes
{
yes &
insert( char );
template< typename xWhat >
yes &
insert( const xWhat & );
typedef int type;
};
struct no
{
};
int
main()
{
cout
<< supports_syntax< yes, syntax_insert< char > >::value
<< endl;
cout
<< supports_syntax< no, syntax_insert< char > >::value
<< endl;
cout
<< supports_syntax< yes, syntax_insert< const float & > >::value
<< endl;
cout
<< supports_syntax< no, syntax_insert< const float & > >::value
<< endl;
cout
<< supports_syntax< yes, syntax_type >::value
<< endl;
cout
<< supports_syntax< no, syntax_type >::value
<< endl;
}
If I am using boost, then how should I write this program?
As it sits, this program is using SFINAE to determine whether
or not a type supports particular syntax. I suspect that there
is functionality in boost to do this.
I have found mpl::has_xxx, which I suspect of being part of the
solution. I've also found type_traits::has_nothrow_constructor
(etc.), which I suspect of using the solution: unfortunately, those
implementations are rotten with unfamiliar macros, which makes
them hard to follow.
What I'm looking for is an approachable sample of using the
relevant boost facility.
*/
#include <fstream>
using std::cout;
using std::endl;
template< typename x, typename xSyntax >
struct supports_syntax
{
private:
typedef char ret_true;
struct ret_false{ char c[2]; };
template< typename xTested >
static
ret_false
func( ... );
template< typename xTested >
static
ret_true
func( typename xSyntax::template match< xTested > * );
public:
static
const bool
value
= ( sizeof( func< x >( 0 ) ) == sizeof( ret_true ) );
};
template< typename xParm >
struct syntax_insert
{
template< typename y, y & (y::*)( xParm ) = &y::insert >
struct match;
};
struct syntax_type
{
template< typename y, typename xType = typename y::type >
struct match;
};
struct yes
{
yes &
insert( char );
template< typename xWhat >
yes &
insert( const xWhat & );
typedef int type;
};
struct no
{
};
int
main()
{
cout
<< supports_syntax< yes, syntax_insert< char > >::value
<< endl;
cout
<< supports_syntax< no, syntax_insert< char > >::value
<< endl;
cout
<< supports_syntax< yes, syntax_insert< const float & > >::value
<< endl;
cout
<< supports_syntax< no, syntax_insert< const float & > >::value
<< endl;
cout
<< supports_syntax< yes, syntax_type >::value
<< endl;
cout
<< supports_syntax< no, syntax_type >::value
<< endl;
}