2

I have the following template function:

template<typename T> std::vector<T> foo::bar(std::vector<T> baz) {
    if (!std::numeric_limits<T>::is_integer) {
        return baz;
    } else {
        for(int i = 0; i < baz.size(); ++i) {
             baz[i] <<= 1;
        }
        return baz;
}

If T is a double the compiler will complain

'<<' illegal, left operand has type 'double'

Eventhough the shift operation will never be called.

Is there a way to inform the compiler that the shift is never going to be called?

Arrrow
  • 542
  • 5
  • 21
  • template specialisation? – UKMonkey Oct 26 '17 at 09:42
  • In C++17, you can use `if constexpr`. For earlier versions of C++, you need to use template specialisation of the function, and not rely on `if` to do the check. – Peter Oct 26 '17 at 09:46
  • Apparently visual studio 2015 doesn't support most of C++17, so I'll have to use template specialisation. – Arrrow Oct 26 '17 at 11:23

0 Answers0