Boost logo

Boost :

Subject: Re: [boost] detecting a constructor with a specific signature
From: Daniel Frey (d.frey_at_[hidden])
Date: 2010-01-27 11:47:59


On 27.01.2010, at 15:52, Kenny Riddile wrote:

> Ya, I was already using the "flag typedef" method, but was just wondering if something non-intrusive was feasible...maybe it isn't. A compiler with 0x support isn't an option I'm afraid.

No need for C++0x if you only need a special case. Requires a good compiler, though, GCC 4.3 is not good enough, 4.4+ is. The following compiles with GCC 4.4, -ansi -pedantic, replacing decltype with sizeof :)

#include <string>
#include <iostream>

struct Foo {
  Foo( const std::string& );
};

struct Bar {
  Bar( const std::string& );
  Bar( const std::string&, Foo& );
};

template< typename T >
T make();

template< int >
struct result { typedef double type; };

template< typename T >
typename result< sizeof T( make< const std::string& >(), make< Foo& >() ) >::type select( int );

template< typename >
char select( ... );

template< typename T >
struct has_foo_ctor
{
  enum { value = sizeof select< T >( 0 ) > 1 };
};

int main()
{
  std::cout << has_foo_ctor< Foo >::value << std::endl;
  std::cout << has_foo_ctor< Bar >::value << std::endl;
}

Regards, Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk