invalid initialization of non-const reference of type ‘Node*&’ from an rvalue of type ‘std::atomic<Node*>::__pointer_type {aka Node*}’ - c++11

I have the following snippet of code:
struct Node {
int data;
Node *next;
};
atomic<Node*> head;
atomic<Node*> temp1 = head.load();
..
Node *temp2 = new Node;
//initialise values
head.compare_exchange_strong(temp1, temp2);
However, I get the following error:
invalid initialization of non-const reference of type ‘Node*&’ from an rvalue of type ‘std::atomic::__pointer_type {aka Node*}’.
I am not getting which reference is constant here. Any help will be appreciated.

Simple answer is that temp1 should be a Node*, not an Atomic,
as cmp/xchg takes two simple type variables.
But I don't really understand what you are trying to achieve.
Surely if you want next to be protected against threading, then it should be declared Atomic inside the struct?

Related

why iterating over unordered_map with unique_ptr forces a pair with a key that is const?

I found a compiler error that I can not understand why it happens only for std::map with std::unique_ptr.
Assume we have the follow unoredered map object and an iteration code for it:
std::unordered_map<uint32_t, std::shared_ptr<char>> map;
for (const std::pair<uint32_t, std::shared_ptr<char>>& item : map)
{
// do something
}
That's compiled well, but if we use unique pointer instead the shared pointer as follows, then we get a compiler error about the iteration pair type:
std::unordered_map<uint32_t, std::unique_ptr<char>> map;
for (const std::pair<uint32_t, std::unique_ptr<char>>& item : map)
{
// do something
}
error C2440: 'initializing': cannot convert from 'std::pair<const _Kty,_Ty>' to
'const std::pair<uint32_t,std::unique_ptr<char,std::default_delete<_Ty>>>
Following this error, we can just add 'const' to the key type and it is compiled.
for (const std::pair<const uint32_t, std::unique_ptr<char>>& item : map)
^^^
|||
Why is this compiling error happens just for unique pointer?
A friend gave me the answer and I'm sharing.
The answer lies in the concept of implicit conversion(copy) a compiler is allowed to do.
Lets see the follow simple example:
const char x = 4;
const char& r = x; // r is reference for x. Checking their addresses yields the same
// address.
const int& ir = c; // implicit creation of object + conversion(copy).
// ir is different type of x, therefor compiler does implicit
// conversion(copy): it creates behind the scene an object of int,
// convert x into this temporary object. The temporary int object is
// then bound to the reference ir. Checking addresses of ir and x
// yields different addresses because ir is reference of the temporary
// object, not to x
So even we use reference - planning for just pointing on an existing object,
we may actually have an object construction + copy (if the types are different and there is conversion between them).
The same happens in the loop I gave in the question:
std::unordered_map<uint32_t, std::shared_ptr<char>> map;
for (const std::pair<uint32_t, std::shared_ptr<char>>& item : map)
{
// do something
}
while the real object held by the map is of type of
std::pair<const uint32_t, std::shared_ptr<char>>
the loop uses reference of different type:
std::pair<uint32_t, std::shared_ptr<char>>
and therefor, behind the scene, for every iteration, a new temporary object is implicitly constructed and a copy operation for conversion is done.
Not only that it isn't efficient, but it also failed to be compiled when the value is unique_ptr because the conversion does copy and unique_ptr can not be copied.
Thats why using 'auto' may save you such mistakes,
std::unordered_map<uint32_t, std::shared_ptr<char>> map;
for (const auto& item : map)
{
// do something
}
I myself sometimes prefer to play and use the explicit form instead 'auto' in order to face such problems and learn :)

Shared pointer to incomplete needs deleter in reset method

I am working with shared_ptr storing pointers of a C library.
Here an example of such a C library containing the header bar.h:
#pragma once
typedef struct Flupp MyFlupp;
MyFlupp *
create_flupp();
void
del_flupp(MyFlupp * fp);
void
print_flupp(MyFlupp * f);
Here the struct has a forward declaration and is defined in the bar.so.
I am using the bar.so in my C++ code:
#include <memory>
extern "C"{
#include "bar.h"
}
int main()
{
std::shared_ptr<MyFlupp> flupp_ptr(nullptr, del_flupp);
flupp_ptr.reset(create_flupp());
print_flupp(flupp_ptr.get());
return 0;
}
Here I am storing the MyFlupp* in a shared_ptr. On the declaration, MyFlupp* is unknown and set to nullptr. Later I am calling the reset operation to set the valid pointer. But when I am compling the code, I get the following error:
In file included from /usr/include/c++/8/bits/shared_ptr.h:52,
from /usr/include/c++/8/memory:81,
from test_foo.cpp:1:
/usr/include/c++/8/bits/shared_ptr_base.h: In instantiation of ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Yp*) [with _Yp = Flupp; <template-parameter-2-2> = void; _Tp = Flupp; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’:
/usr/include/c++/8/bits/shared_ptr_base.h:1293:4: required from ‘std::__shared_ptr<_Tp, _Lp>::_SafeConv<_Yp> std::__shared_ptr<_Tp, _Lp>::reset(_Yp*) [with _Yp = Flupp; _Tp = Flupp; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2; std::__shared_ptr<_Tp, _Lp>::_SafeConv<_Yp> = void]’
test_foo.cpp:10:35: required from here
/usr/include/c++/8/bits/shared_ptr_base.h:1126:19: error: invalid application of ‘sizeof’ to incomplete type ‘Flupp’
static_assert( sizeof(_Yp) > 0, "incomplete type" );
When I am providing the deleter to the reset operation than it is working.
flupp_ptr.reset(create_flupp(), del_flupp);
Can anybody explain me whats going on? I already looked #cppreference but I does not found an answer.
The problem is that the type Flupp has only been forward-declared, but not defined. In the context of the use here, it is considered an incomplete type.
This has certain implications for the use with std::shared_ptr:
std::shared_ptr may be used with an incomplete type T. However, the
constructor from a raw pointer (template<class Y> shared_ptr(Y*)) and
the template<class Y> void reset(Y*) member function may only be
called with a pointer to a complete type (note that std::unique_ptr
may be constructed from a raw pointer to an incomplete type).
Source: cppreference.com
Instead you need to use the respective overloads that accept a pointer and the deleter as arguments.
With unique_ptr this is not necessary, as that one stores the custom deleter as part of the type. But with shared_ptr the deleter is type-erased and only recovered at runtime. This allows you to change the deleter of an existing shared_ptr when calling reset. For this reason you always need to re-state which deleter to use whenever you're calling reset. If no deleter is given, each call to reset will also implicitly reset the deleter to just calling delete on the managed pointer.
So to make it work, just change your reset call to
flupp_ptr.reset(create_flupp(), del_flupp);

error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]

I am working on a linux kernel module.
A struct tcpsp_conn is defined in the header file as follows:
struct tcpsp_conn {
...
struct timer_list timer; /* exp. timer*/
...
};
Then I declare a pointer to the structure and try to assign the function:
struct tcpsp_conn *cp;
cp->timer.function = tcpsp_conn_expire;
tcpsp_conn_expire function is defined in the same way as in the struct timer_list of the kernel:
static void tcpsp_conn_expire(unsigned long data)
I don't understand why am I getting this error:
error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
cp->timer.function = tcpsp_conn_expire;
It doesn't look to have a problem with types.
Type of your tcpsp_conn_expire function differs from the type of .function field of the timer_list structure.
In the newest kernel (since 4.15) this function-field is declared with struct timer_list * argument instead of unsigned long, as follows:
struct timer_list {
...
void (*function)(struct timer_list *);
...
};
Having such argument, you may obtain the pointer to the struct tcpsp_conn structure, into which the timer is embedded, with macro container_of.

initialize a string size during declaration

suppose I have a class like this and I would like to create a string with a
specific capacity. I tried doing the following but that did not work.
Any suggestions ? I know I could do it in the constructor but would like to do it during the declaration if possible.
class foo
{
std::string bar = std::string().resize(45);
}
I get the error
main.cpp: In function 'int main()':
main.cpp:8:46: error: conversion from 'void' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string}' requested
std::string test = std::string().resize(45);
In C++, you probably don't want to "chain" methods unless previous methods have the correct return type.
As suggested by #James Maa, you can do simply use the constructor.
In c++11 we have new feature called move constructor so
string str = string();
doesn't cause extra time.
http://en.cppreference.com/w/cpp/language/move_constructor
with move constructor, the program would directly use the address of the temporary constructed string after = sign, without making a copy.
The problem is that resize()function in c++ actually returns void
basic_string( size_type count,
CharT ch,
const Allocator& alloc = Allocator() );
This constructor might be something you are directly interested in.
You can do something with
std::string str(45, ' ');

'boost shared_ptr' and 'boost lock' together = messed up

I am new to both concepts shared_ptr and mutex (boost or not boost). I am trying to use it in my classes :
typedef boost::shared_mutex Lock;
typedef boost::unique_lock< Lock > WriteLock;
typedef boost::shared_lock< Lock > ReadLock;
class subscriptionInfo
{
public:
//this is not a copy constructible class. so I have to use shared pointer
boost::shared_ptr<Lock> myLock;
...
}
...
std::vector<DATA_MSG_PTR>& subscriptionInfo::getIncoming() {
ReadLock Lock(myLock);
return incoming;
}
and the error says:
error: no matching function for call to ‘boost::shared_lock<boost::shared_mutex>::shared_lock(boost::shared_ptr<boost::shared_mutex>&)’
I will appreciate if you help me find out what I messed up and how to solve it.
thanks
The myLock member is a pointer. A smart one but in any case a pointer. The shared_lock constructor accepts a reference to a mutex object and not a pointer. That is why the error message takes place. To solve the problem you have to dereference the pointer as ReadLock Lock(*myLock);

Resources