Why is this simple program using std :: rotate not compiling?

This does not work:

#include <algorithm> int main() { int a[] = { 1, 2, 3 }; auto it = std::rotate(std::begin(a), std::begin(a) + 1, std::end(a)); } 

The error I am getting is:

 main.cpp:6:10: error: variable has incomplete type 'void' auto it = std::rotate(std::begin(a), std::begin(a) + 1, std::end(a)); 

This is clearly the wrong behavior as the rotation declaration:

 template<class ForwardIterator> ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); 

Why is this simple program not compiling?

+5
source share
2 answers

Prior to C ++ 11, std::rotate used to return void . Thus, you are most likely running a non-C ++ version.

+6
source

This looks like a libstdc++ problem, which we can see by running a test using both libstdc++ and libc++ , and we can see that it only fails when we use libstdc++ .

Using replicators of online compilers makes the quick test quite simple, lib ++ live version does not create errors. While the version of libstdc ++ live generates the following error:

error: variable has an incomplete type 'void'

If we look at the cppreference entry for std :: rotate , we can see that the pre C ++ 11 version really returned void :

 template< class ForwardIt > void rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ) (until C++11) template< class ForwardIt > ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); (since C++11) 

As stated in the comments above, this is apparently a known bug :

 25.3 | Mutating sequence operations | Partial rotate | returns void. 

In addition, it may be worth noting Visual Studio without problems with this code.

+3
source

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


All Articles