मैं इसी तरह की समस्या के साथ आया था जब तर्कसंगत हैं, जब तर्क संगत होते हैं, और कुछ और करने के लिए नहीं करते हैं।
यहाँ एक सामान्यीकृत लक्षण है MSVC 2013 को काम कर (सी ++ 11 सामान की आवश्यकता है):
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 {};
};
यहाँ एक लक्षण के उपयोग का उदाहरण है, मैं छोड़ने एक व्यायाम के रूप enable_if आवेदन: डी:
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)"
);
स्रोत
2015-02-05 19:22:50
आपका मतलब वस्तुओं को बनाने की कोशिश किए बिना है? – Naveen
यदि आप इस कन्स्ट्रक्टर का उपयोग करते समय उपयोग नहीं करते हैं, तो संकलक एक त्रुटि उत्पन्न करेगा। यदि आप इसका उपयोग नहीं करते हैं, तो आप इसे क्यों पेश करना चाहते हैं? –
किसी प्रस्तावित चेक के लिए दिलचस्प टेस्टकेस std :: स्ट्रिंग स्वयं है। – MSalters