[Boost-users] [mpl] is_constructible_from

24 views
Skip to first unread message

Roman Perepelitsa

unread,
Mar 13, 2009, 4:48:25 AM3/13/09
to boost...@lists.boost.org
I need a metafunction that returns true iif a type has a constructor with 2 arguments of a given type. Does mpl have it? If not, is it possible to implement?

template <class C, class T1, class T2>
struct is_constructible_from;

struct foo { foo(int, int); };
struct bar { bar(int*, int); };
struct baz {};

int main() {
  assert(is_constructible_from<foo, int, int>::value);
  assert(is_constructible_from<foo, int, double>::value);
  assert(!is_constructible_from<bar, int, int>::value);
  assert(!is_constructible_from<baz, int, int>::value);
};

Roman Perepelitsa.

David Abrahams

unread,
Mar 16, 2009, 8:32:11 PM3/16/09
to boost...@lists.boost.org

I know of no way to do that.

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

joel falcou

unread,
Mar 17, 2009, 2:27:40 AM3/17/09
to boost...@lists.boost.org
> I know of no way to do that.

Can't the good'ol sizeof trick work in this situation ?

Steven Watanabe

unread,
Mar 17, 2009, 11:50:50 AM3/17/09
to boost...@lists.boost.org
AMDG

How do you get an expression to have
a different type depending on whether
a given class has a particular constructor?

In Christ,
Steven Watanabe

Ovanes Markarian

unread,
Mar 17, 2009, 12:15:53 PM3/17/09
to boost...@lists.boost.org
Some time ago, I needed a trait is_copy_constructible, which is a special case of is_constructible_from. I remeber to have some thought, that if C++ would support injected friends it might be possible to calculate. But that was really tricky solution and I just posponed it until the compilers support injected friends (due to the upcoming standard). At least MSVC 9 supports it now and I think we could try solving it, may be for the first for copy constructible trait and later generalize it.


On Tue, Mar 17, 2009 at 4:50 PM, Steven Watanabe <watan...@gmail.com> wrote:
AMDG

Noah Roberts

unread,
Mar 17, 2009, 12:20:15 PM3/17/09
to boost...@lists.boost.org
Roman Perepelitsa wrote:
> I need a metafunction that returns true iif a type has a constructor
> with 2 arguments of a given type. Does mpl have it? If not, is it
> possible to implement?

As has been explained it's impossible. However, if you change the
construction syntax of your objects you could do it. If you made the
constructors private or protected and provided a static function for
building them you'd have something that can be tested with metaprogramming.

Not saying this will help you, but it's an idea.

Roman Perepelitsa

unread,
Mar 17, 2009, 12:37:03 PM3/17/09
to boost...@lists.boost.org
Since it's impossible to implement is_constructible_from for constructors with two arguments, I'll go with a workaround that does not require me to have such metafunction. Thanks to everyone who replied!

Roman Perepelitsa.
Reply all
Reply to author
Forward
0 new messages