Consider the following code (should be able to copy-paste directly into godbolt):
#include <type_traits>
template<typename T>
constexpr bool isInt() {
return std::is_base_of<int, std::remove_reference_t<T>>::value;
}
template<typename T>
constexpr bool isInt(const T& t) {
return isInt<T>();
}
class myClass {
public:
template<typename T1>
bool doThing(const T1& t1) {
constexpr bool x1 = isInt(t1); // compile error
constexpr bool x2 = isInt<decltype(t1)>();
constexpr bool x3 = isInt(1);
constexpr bool x4 = isInt<int>();
return false;
}
};
int main() {
myClass x;
x.doThing(1);
return 0;
}
In this scenario everything compiles fine except for the "x1" assignment. The error I get is "'t1' is not a constant expression".
In this case, the only reason I'm passing in "t1" is for the convenience of having the function deduce the type for me. But apparently that's not possible? I don't have a great understanding of how constexpr works, so I'm hoping to understand why this is problem.
Thanks!
EDIT:
Forgot to add this, but if the method is not templated, then it will compile. AKA:
bool doThing2(const int i) {
constexpr bool y1 = isInt(i);
return y1;
}
Compiles fine.