Consider below code:
template<typename T>
void f(T a[])
{
if(sizeof(T) > 1)
for(...)a[i] = j
else
memset(a, j, ...
}
I think compiler should remove one of the if-branches in compile-time.
Is it true?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since the compiler can decide if
(sizeof(T) > 1)will return true or not, at compile time itself, the compiler can emit machine code without any branch if compiled with optimization flag on. So the resultant code will have eitherforloop (or an equivalent code generated out of it), ormemset. In other words, the resultant code will not haveif-elseblock (provided you’re using a smart compiler).