#include <iostream>
#include <utility>
#include <type_traits>
using namespace std;
struct DD {
DD() { cout << "DD() ";};
// DD(const DD&) { cout << "DD(const DD&) "; };
// DD(int,char) { cout << "DD(int,char) "; };
// DD(size_t,char) { cout << "DD(size_t,char) "; };
~DD() { cout << "~DD() "; };
};
template <typename T> struct AA {
T m_val;
void f() {
m_val.~T();
if constexpr (is_constructible<T, int, char>::value) {
cout << "new1: ";
new (&m_val) T(int(1), char(2));
} else if constexpr (is_constructible<T, size_t, char>::value) {
cout << "new2: ";
new (&m_val) T(size_t(1), char(2));
} else if constexpr (is_copy_constructible<T>::value) {
cout << "new3: ";
new (&m_val) T(m_val);
} else if constexpr (is_constructible<T, const T&>::value) {
cout << "new4: ";
new (&m_val) T(m_val);
} else if constexpr (is_default_constructible<T>::value) {
cout << "new5: ";
new (&m_val) T();
} else {
cout << "Fail\n";
}
};
};
int main()
{
AA<DD> x;
x.f();
cout << "\n";
return 0;
}
-------------------
Thanks, Bo Persson and Paavo Helde. Tested fine.
I did not know the syntax " if constexpr (is_constructible<T,int,char>::value)".