Disclaimer: I am not very accustomed with working with C++11, especially with new semantics like auto
and move
.
I am trying to keep a O(log(n))
random access memory structure of simple struct
s sorted with respect to their first elements, with O(1)
access to min/max elements. C++11 STL's set
class provides exactly this, so tried implementing my design with it using g++ (GCC) 7.3.0 and preparing an overload for operator<
. Since typing templated types every time is pain, I used auto
for the result of the std::set::find
member function as follows:
Minimal sample:
#include <set>
template <typename X> struct _get_auto_type;
struct A {
int x;
char y;
bool operator<(const A &a) const { return x < a.x; }
};
int main(void)
{
typedef std::set<A> A_set;
A_set a_set{{1, 'a'}, {0, 'b'}, {2, 'c'}};
auto a_it = a_set.find({0, 'b'});
a_it->y++;
_get_auto_type<decltype(a_it)> _gat;
return 0;
}
g++ output:
$ g++ -std=c++11 possible_gcc_bug.cpp
possible_gcc_bug.cpp: In function ‘int main()’:
possible_gcc_bug.cpp:16:34: error: aggregate ‘_get_auto_type<std::_Rb_tree_const_iterator<A> > _gat’ has incomplete type and cannot be defined
_get_auto_type<decltype(a_it)> _gat;
^~~~
possible_gcc_bug.cpp:17:10: error: increment of member ‘A::y’ in read-only object
a_it->y++;
^~
The two cases where this result is expected are:
- Compiler has a bug (my initial guess), or
std::set
'sKey
values are rvalues only, which seems more reasonable but I couldn't find any solid evindence for this.
Are either of my guesses correct, and if the 2nd one is true, which data structure (preferably from STL) can I use instead?