Click here to Skip to main content
14,331,020 members

C / C++ / MFC

 
GeneralRe: error C2059 Pin
Richard MacCutchan21-Aug-19 0:03
protectorRichard MacCutchan21-Aug-19 0:03 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 0:52
member_Flaviu21-Aug-19 0:52 
GeneralRe: error C2059 Pin
Victor Nijegorodov21-Aug-19 2:08
memberVictor Nijegorodov21-Aug-19 2:08 
GeneralRe: error C2059 Pin
Victor Nijegorodov21-Aug-19 2:17
memberVictor Nijegorodov21-Aug-19 2:17 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 2:38
member_Flaviu21-Aug-19 2:38 
GeneralRe: error C2059 Pin
Richard MacCutchan21-Aug-19 3:59
protectorRichard MacCutchan21-Aug-19 3:59 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 20:50
member_Flaviu21-Aug-19 20:50 
GeneralRe: error C2059 Pin
Stefan_Lang21-Aug-19 5:24
memberStefan_Lang21-Aug-19 5:24 
Took me a while to figure it out, but decltype(*pos) will give you a reference type, and the parts of the macro using this type to cast to type* then fail, because you can't create a pointer to a reference!

However, that's where std::remove_reference comes to the rescue! Try these definitions:
#include <type_traits> // for std::remove_reference

using namespace std;

#define list_entry(ptr, type, member) \
	((type *)((char *)(ptr)-(size_t)(&((type *)nullptr)->member)))
#define list_next_entry(pos, member)		\
	list_entry((pos)->member.next, remove_reference< decltype(*(pos)) >::type, member)

P.S.: are you sure the second #define you posted is correct? I could make up some type definitions that I could use with these macros without encountering compiler errors, but I'm not at all sure the macros would provide a valid pointer for these types!

Here's the code I used to validate there are no compiler errors:
struct link
{
    struct link* next;
};
struct node
{
   int a;
   link b;
   int c;
};
int main()
{
    node y = { 3, nullptr, 2 };
    node x = { 5, &y.b, 4 };
    node* p = list_next_entry(&x, b);
    cout << "value = " << p->a << endl;

    return 0;
}
It did compile, but the output was some random number, not 3 as I expected. I suspect that either the structs or that second #define must be defined differently...

P.P.S.: it compiles and prints value = 3 as expected.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)


modified 21-Aug-19 11:44am.

GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 21:45
member_Flaviu21-Aug-19 21:45 
GeneralRe: error C2059 Pin
Stefan_Lang21-Aug-19 21:51
memberStefan_Lang21-Aug-19 21:51 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 23:53
member_Flaviu21-Aug-19 23:53 
AnswerRe: error C2059 Pin
Stefan_Lang21-Aug-19 3:08
memberStefan_Lang21-Aug-19 3:08 
QuestionC++ Program to decompress a compressed string Pin
antoniu20016-Aug-19 6:19
memberantoniu20016-Aug-19 6:19 
AnswerRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 1:19
protectorRichard MacCutchan17-Aug-19 1:19 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 1:39
memberantoniu20017-Aug-19 1:39 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 3:02
protectorRichard MacCutchan17-Aug-19 3:02 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 3:12
memberantoniu20017-Aug-19 3:12 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 3:22
protectorRichard MacCutchan17-Aug-19 3:22 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 3:24
memberantoniu20017-Aug-19 3:24 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 3:59
protectorRichard MacCutchan17-Aug-19 3:59 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 4:02
memberantoniu20017-Aug-19 4:02 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 4:19
protectorRichard MacCutchan17-Aug-19 4:19 
GeneralRe: C++ Program to decompress a compressed string Pin
David Crow19-Aug-19 4:49
memberDavid Crow19-Aug-19 4:49 
AnswerRe: C++ Program to decompress a compressed string Pin
Patrice T17-Aug-19 19:16
mvePatrice T17-Aug-19 19:16 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20018-Aug-19 2:42
memberantoniu20018-Aug-19 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.