C++ std::sort of const struct - sorting

I have the following struct in a vector
struct ub_node {
const size_t index;
const double ub_dist;
bool operator<(const ub_node &rhs) const { return ub_dist< rhs.ub_dist; }
};
I would like to sort that vector. I have tried using std::sort but I get a compile error:
error: use of deleted function ‘ub_node& ub_node::operator=(ub_node&&)’ with a reference to the line where I do std::sort(result.begin(), result.end());, where result is of type vector<ub_node>.
As far as I understand, the const does NOT affect the execution time, but merely ensures that the programmer (me) does not do anything stupid. If this is the case, I might remove the const and try to ensure that I do not change the node afterwards. Can someone confirm this? Or help me to sort it?

As far as I understand, the const does NOT affect the execution time, but merely ensures that the programmer (me) does not do anything stupid.
Correct.
Sorting a vector swaps the elements around, which is not possible if the elements have immutable data (with your ub_node type you can't even remove or insert an element anywhere except the end of the vector, because that also needs to modify existing elements).
I suggest you remove the const from your members, so the type is possible to modify, and then structure your code so that parts of the program which shouldn't modify it only interacts with the class through const ub_node& or const ub_node*.
That means that the class is modifiable when necessary, but won't be modified by parts of the program that aren't supposed to modify it.

Related

Iterating over const_iterator

ALL,
I have a function with the following signature:
void foo(const std::vector<Bar *> &myvec);
Inside this function I need to loop thru the members of the vector and perform some operations.
So, I tried this:
for( std::vector<Bar *>::const_iterator it = myvec.begin(); it < myvec.end(); ++it )
{
// modify properties of Bar * pointer
(*it)->SetSomeValue( baz );
}
however this code asserts since the iterator is constant.
Now obviously the vector is constant, which means that the function shouldn't be modifying myvec.
What's the best solution here?
Can I use const_cast here to remove constness? It would be kind of hack-ish, but if it works.
But I feel there must be a better solution.
TIA!!
You should use the myvec.cbegin() method instead of myvec.begin(), to ensure that you are not modifying the object the iterator points to.
Of course, for myvec.end(), use myvec.cend() accordingly.
The iterator itself doesn't need to be a const_iterator, in the contrary, you want to modify the objects it gives you - set_...() sounds like a non-const activity.

How can I use boost::interprocess::private_node_allocator on a recursive datastructure?

I have a recursive datastructure I want to store in shared memory. Thus far I have got this (simplified):
using namespace boost::interprocess;
typedef allocator<wchar_t,
managed_shared_memory::segment_manager>TCharAllocator;
typedef basic_string<wchar_t, std::char_traits<wchar_t>, TCharAllocator> MyShmString;
class Node;
template<>
struct sizeof_value<Node>
{
static const std::size_t value = *NodeSize*; <---??
};
typedef private_node_allocator<Node, managed_shared_memory::segment_manager> TNodeAllocator;
class Node
{
private:
MyShmString _value;
vector<Node, TNodeAllocator> _children;
};
If you fill in a number for Nodesize it compiles and runs. You can get this number by compiling and printing a sizeof(Node). This is not very robust. I have tried using a forward declared constexpr function returning the size of Node, but this does not compile (on MSVC).
Is there another way to solve this?
If I understand correctly the only source of complication is with declaring datastructures that entail incomplete types.
If the boost::interprocess::sizeof_value<> specialization works, that would be enough.¹
In terms of robustness, it is easy to add a static_assert after the definition of Node has become complete to ensure that the actual value matches the size reported by the compiler:
static_assert(boost::interprocess::sizeof_value<Node>::value == sizeof(Node), "sizeof mismatch");
¹ I have a hunch it can be done without that, but I cannot spend the time to make your code self-contained right now to check

Passing const reference or shared pointer / vector and assign to non-reference member

CPP check is complaining about this construction:
void setThing(std::shared_ptr<Thing> theThing)
{
memberThing = theThing;
}
where memberThing is a std::shared_ptr<Thing>.
When changing to:
void setThing(const std::shared_ptr<Thing>& theThing);
cppcheck is not complaining anymore.
I need a shared pointer, I can't guarantee theThing will exist forever, so I don't want to have a member reference. But why is a const reference working when assigning it to a not-reference member? And what happens actually? Is the shared pointer copied to memberThing? Where did the reference go?
And actually exactly the same question for
void setThings(std::vector<std::shared_ptr<Thing>> theThings)
{
memberThings = theThings;
}
where memberThing is a std::vector<std::shared_ptr<Thing>>.
When using this construction, what is happening with the vector? Is it copied? What happens when the original vector is destroyed? Can I better use the first version or the second?
I am quite confused a const vector& can be assigned to a vector (not-reference). Is that a good idea anyway? What is the best thing to do in this situation? And why?

Is there a way to make a moved object "invalid"?

I've some code that moves an object into another object. I won't need the original, moved object anymore in the upper level. Thus move is the right choice I think.
However, thinking about safety I wonder if there is a way to invalidate the moved object and thus preventing undefined behaviour if someone accesses it.
Here is a nice example:
// move example
#include <utility> // std::move
#include <vector> // std::vector
#include <string> // std::string
int main () {
std::string foo = "foo-string";
std::string bar = "bar-string";
std::vector<std::string> myvector;
myvector.push_back (foo); // copies
myvector.push_back (std::move(bar)); // moves
return 0;
}
The description says:
The first call to myvector.push_back copies the value of foo into the
vector (foo keeps the value it had before the call). The second call
moves the value of bar into the vector. This transfers its content
into the vector (while bar loses its value, and now is in a valid but
unspecified state).
Is there a way to invalidate bar, such that access to it will cause a compiler error? Something like:
myvector.push_back (std::move(bar)); // moves
invalidate(bar); //something like bar.end() will then result in a compiler error
Edit: And if there is no such thing, why?
Accessing the moved object is not undefined behavior. The moved object is still a valid object, and the program may very well want to continue using said object. For example,
template< typename T >
void swap_by_move(T &a, T &b)
{
using std::move;
T c = move(b);
b = move(a);
a = move(c);
}
The bigger picture answer is because moving or not moving is a decision made at runtime, and giving a compile-time error is a decision made at compile time.
foo(bar); // foo might move or not
bar.baz(); // compile time error or not?
It's not going to work.. you can approximate in compile time analysis, but then it's going to be really difficult for developers to either not get an error or making anything useful in order to keep a valid program or the developer has to make annoying and fragile annotations on functions called to promise not to move the argument.
To put it a different way, you are asking about having a compile time error if you use an integer variable that contains the value 42. Or if you use a pointer that contains a null pointer value. You might be succcessful in implementing an approximate build-time code convention checker using clang the analysis API, however, working on the CFG of the C++ AST and erroring out if you can't prove that std::move has not been called till a given use of a variable.
Move semantics works like that so you get an object in any it's correct state. Correct state means that all fields have correct value, and all internal invariants are still good. That was done because after move you don't actually care about contents of moved object, but stuff like resource management, assignments and destructors should work OK.
All STL classes (and all classed with default move constructor/assignment) just swap it's content with new one, so both states are correct, and it's very easy to implement, fast, and convinient enough.
You can define your class that has isValid field that's generally true and on move (i. e. in move constructor / move assignment) sets that to false. Then your object will have correct state I am invalid. Just don't forget to check it where needed (destructor, assignment etc).
That isValid field can be either one pointer having null value. The point is: you know, that object is in predictable state after move, not just random bytes in memory.
Edit: example of String:
class String {
public:
string data;
private:
bool m_isValid;
public:
String(string const& b): data(b.data), isValid(true) {}
String(String &&b): data(move(b.data)) {
b.m_isValid = false;
}
String const& operator =(String &&b) {
data = move(b.data);
b.m_isValid = false;
return &this;
}
bool isValid() {
return m_isValid;
}
}

Techniques for finding which member prevents creation of move constructor/assignment operator

I have a situation where profiling has shown that a large structure is taking a significant time to be copied:
std::vector<LargeStruct> allOutputs;
std::vector<LargeStruct> thisOutput(getResultOfOperation());
// catenate results to allOutputs
std::copy(thisOutput.begin(), thisOutput.end(), std::back_inserter(allOutputs));
My first instinct was to try changing copy to move:
std::move(thisOutput.begin(), thisOutput.end(), std::back_inserter(allOutputs));
However this didn't yield any performance increase, presumably because one of the members of LargeStruct doesn't have a move constructor/assignment operator.
The question is: how can I diagnose this to find out which member is causing the problem?
EDIT:
Does std::is_move_constructible help in this case? It sounds from this post that it doesn't
You can probably get the compiler to give you a useful diagnostic by forcing the type to be move only. If you suppress the copy constructor, the compiler will have to use the move constructor, and if it can't generate it, it will probably tell you why. You can suppress the copy constructor by either explicitly suppressing the copy operations:
struct LargeStruct {
LargeStruct(LargeStruct const &) = delete;
LargeStruct &operator=(LargeStruct const &) = delete;
// ...
};
Or you could add a move-only member to your class:
struct LargeStruct {
unique_ptr<char> dummy;
// ...
};
Either of these should force LargeStruct to be a move-only type and produce a diagnostic if the move operations can't be generated. If the error messages aren't informative, you could try the same test on each (nonstatic) data member of LargeStruct to find out which is/are copy-only.

Resources