Valid C ++ 03 template code will not compile in C ++ 11

I ran into a small problem (easily solvable) when writing C ++ 03 template code, which usually compiles, which will not compile when using the C ++ 11 dialect.

The problem occurs when resolving template parameters. Let this code be an example of this:

template <uint32_t number>
struct number_of_bits {
    enum  {
        value = 1 + number_of_bits<number >> 1>::value
    };
};

template <>
struct number_of_bits<0> {
    enum  {
        value = 0
    };
};

Since C ++ 11 now allows "→" to complete the list of template parameters, which takes the template parameter as the last argument, it creates a problem when analyzing this code.

I use GCC (version 4.8.1) as my compiler, and it usually compiles using the command line:

g++ test.cc -o test

But it does not compile when I add a command line switch -std=c++11:

g++ -std=c++11 test.cc -o test

++ 11 GCC? , ?

+4
1

Clang++ -std=c++03:

test.cpp:6:43: warning: use of right-shift operator ('>>') in template argument
      will require parentheses in C++11 [-Wc++11-compat]
        value = 1 + number_of_bits<number >> 1>::value
                                          ^
                                   (          )

, ++ 11 , >> . , :

value = 1 + number_of_bits<(number >> 1)>::value
+7

Source: https://habr.com/ru/post/1598159/


All Articles