SP() : t{ new T{} } { }
std::enable_if_t<
std::is_class_v<T>, T
>
* operator-> ();
T& operator* () {
return *t;
}
T* t;
};
struct Foo {
int x = 12;
}
SP<Foo> f;
// works fine
auto x = f->x;
//blows up if i do built in type
SP<int> i;
auto z = *i;
when i initialize it with built in types it gives the error no type named type in std::enable_if<false, T>::type
well i think it makes sence due to int is not a class type. but isnt that what enable_is made for? maybe im doin it the wrong way
would this be the right way:
template<typename T,
typename = void>
struct SP {
SP() : t{ new T{} } { }
T& operator* () {
return *t;
}
T* t;
};
template<typename T>
struct SP<T, typename =
enable_if<is_class<T>, T>::type>
{
SP() : t{ new T{} } { }
T* operator-> ();
T& operator* () {
return *t;
}
T* t;
};
struct Foo {
int x = 12;
}
SP<Foo> f;
// works fine
auto x = f->x;
// works fine
SP<int> i;
auto z = *i;
You might want to read about SFINAE because that is why std::enable_if can do what it does.
Обсуждают сегодня