I'm trying to detect if an argument is a reference member, at compile time. It does not catch it and I can't figure it why.
#include <type_traits>
struct a {
a(): i1(42), i2(i1){}
int i1;
int& i2;
};
template <typename T>
void Field(T &field) {
if constexpr (std::is_pointer_v<T>) {
//handle pointer
} else if constexpr (std::is_reference_v<T>) {
static_assert(!std::is_reference_v<T>, "Reference not supported");
} else {
//handle value
}
}
int main()
{
a a_;
Field(a_.i2); // I want this to fail at compile time, but it's not
}
What am I doing wrong?
std::is_reference_vworks in principle, but not the way you're using it. You have a function templateField(T &field), where the template type parameterTis not going to be deduced to a reference.Whatever, you're trying to do here, you could:
std::reference_wrapperand always accept a value parameterT fieldstd::type_identity_t<T> fieldparameter, which forces the user to callField<int&>(a_.i2)manuallyField<decltype(a_.i2)>(a_.i2)