Mi è venuto in mente un problema simile che volesse inoltrare i costruttori alla classe base quando gli argomenti sono compatibili e fare qualcos'altro quando no.
ecco un tratti generalizzate a lavorare su MSVC 2013 (richiede C++ 11 roba):
namespace detail {
template<class type,class...Args>
class constructible_from
{
template<class C>
static C arg();
template <typename U>
static boost::mpl::true_ constructible_test(U *, decltype(U(arg<Args>()...)) * = 0);
static boost::mpl::false_ constructible_test(...);
public:
typedef decltype(constructible_test(static_cast<type*>(nullptr))) result;
};
} // namespace detail
template<class type>
struct constructible
{
template<class...Args>
struct from :
detail::constructible_from<type,Args...>::result {};
};
Ecco un esempio di utilizzo tratti, lascio il enable_if applicazione come un esercizio: D:
struct b{};
struct c{};
struct d : c{};
struct a
{
a() {}
a(a &) {}
a(b,c) {}
a(c) {}
};
static_assert(
constructible<a>::from<>::value,
"a()"
);
static_assert(
constructible<a>::from<a&>::value,
"a(a&)"
);
static_assert(
! constructible<a>::from<const a&>::value,
"a(const a&)"
);
static_assert(
constructible<a>::from<b,c>::value,
"a(b,c)"
);
static_assert(
! constructible<a>::from<b>::value,
"a(b)"
);
static_assert(
constructible<a>::from<c>::value,
"a(c)"
);
static_assert(
constructible<a>::from<d>::value,
"a(d)"
);
fonte
2015-02-05 19:22:50
Intendi senza cercare di creare oggetti? – Naveen
Se si utilizza questo costruttore mentre non è definito, il compilatore genererà un errore. Se non lo usi, allora perché vuoi che sia presente? –
L'interessante testcase per ogni controllo proposto è std :: string stesso. – MSalters