Checking if a class has a copy constructor with TMP
-
28-06-2021 - |
Frage
I've been attempting a little SFINAE at making a way to determine if a generic type T has a copy constructor I can use. Here is where I currently am.
template <bool statement, typename out>
struct Failable
{
typedef out Type;
};
//This class is only used to insert statements that
//could encounter substitution failure
template <typename O>
struct COPY
{
template <typename T>
typename Failable<true == sizeof(&T::T(const T&)), char>::Type copy(int)
{}
template <typename T>
typename Failable<true, int>::Type copy(...)
{}
};
However, this is also where I'm kinda stuck. &T::T(const T&)
is obviously an invalid statement, as we can't provide an argument list with a pointer-to-member, even if a p-t-m-function.
I could always try to specify some sort of void (T::*ptmf)(const T&) = &T::T
, and hope it implicitly determines the right overloaded constructor to put into the pointer to member function, but that also implies that Constructors have a specific return type, which I would have to specify.
Does anyone else have any ideas with which I can trek forth? (I also need to apply a similar concept to checking for an assignment operator.)
Thanks in advanced.
Lösung
You could use std::is_copy_constructible and std::is_assignable for this.
Andere Tipps
You can do with your code, just need to modify it a little bit.
template <bool statement, typename out>
struct Failable
{
typedef out Type;
};
template <typename O>
struct COPY
{
static O MakeO();
template <typename U> // U and T are the same type
static typename Failable<(sizeof U(MakeO())), char>::Type copy(int);
template <typename U>
static typename Failable<true, int>::Type copy(...);
enum { value = sizeof(char) == sizeof( copy<O>(0) ) };
};