I have this snippet.
#include <iostream>
#include <string>
struct JustStr {
JustStr(const std::string& x) : val(x) {}
static constexpr bool pred = false;
std::string val;
};
template <typename T>
class C {
private:
T x;
public:
C(T x_) : x(x_) {}
void f() {
if constexpr (!x.pred) {
std::cout << x.val << std::endl;
}
}
};
template<typename T>
void f2(T x) {
T y(x);
if constexpr (!y.pred) {
std::cout << x.val << std::endl;
}
}
int main() {
C<JustStr> c(JustStr("yes"));
c.f(); // Fails
f2(JustStr("yes")); // Succeeds
return 0;
}
My question is: shouldn't c.f();
and f2(JustStr("yes"));
fail or succeed simultaneously? Why does the one fail and why does the other succeed?