I'm trying to make a function based on input Template. It reads a file.
template<class size> void config::readConfig(char * setting, char * subsetting, size & status) {
char temp[255];
if (!GetPrivateProfileStringA(setting, subsetting, nullptr, temp, 255, cfg)) {
error = true;
}
if (std::is_same<size, char [4]>::value) {
sprintf_s(status, "%s", temp);
} else { status = atof(temp); }
}
I'm basically just checking if the desired input is a char. If it is, then we'll copy the char read over, but if not, we'll copy the bool/int/float/double. Maybe I'm just using std::is_same incorrectly.
My code won't compile because it doesn't look like it recognizes the check and seems like it always returns true.
Your code won't compile because an
ifstatement is a runtime construct. Consider this code:You can talk to me all day about how
bis alwaysfalse, but the compiler still requires the code in thetruecase of theifblock to compile. What's happening in your example for something like anintis this:The compiler does not know how to make
sprintf_scompile whenstatusis anint.The solution is to use overloading:
The thing you're looking for is usually referred to as
static_if(similar to the D conditional compilation construct of a "Static If Condition"). There is no support in C++17 and it isn't planned for C++2a (yet), but you can emulate it easily with the answer to this question.