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?
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 either for loop (or an equivalent code generated out of it), or memset. In other words, the resultant code will not have if-else block (provided you're using a smart compiler).
What is wrong with std::fill ?
std::fill(begin(a), end(a), j); or are you worried that your Standard Library is not optimized enough ?
memset switch, that would perhaps be a good optimization to suggest though.
memset()instead offorloops for some typeT,sizeof(T)is not an appropriate way to determine if it is eligible for such an optimization. You want to useis_pod<T>::valueinstead ofsizeof(T).