Click here to Skip to main content
14,774,771 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: error C2059 Pin
Richard MacCutchan21-Aug-19 1:03
mveRichard MacCutchan21-Aug-19 1:03 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 1:52
Member_Flaviu21-Aug-19 1:52 
GeneralRe: error C2059 Pin
Victor Nijegorodov21-Aug-19 3:08
MemberVictor Nijegorodov21-Aug-19 3:08 
GeneralRe: error C2059 Pin
Victor Nijegorodov21-Aug-19 3:17
MemberVictor Nijegorodov21-Aug-19 3:17 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 3:38
Member_Flaviu21-Aug-19 3:38 
GeneralRe: error C2059 Pin
Richard MacCutchan21-Aug-19 4:59
mveRichard MacCutchan21-Aug-19 4:59 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 21:50
Member_Flaviu21-Aug-19 21:50 
GeneralRe: error C2059 Pin
Stefan_Lang21-Aug-19 6:24
mveStefan_Lang21-Aug-19 6: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 22:45
Member_Flaviu21-Aug-19 22:45 
GeneralRe: error C2059 Pin
Stefan_Lang21-Aug-19 22:51
mveStefan_Lang21-Aug-19 22:51 
GeneralRe: error C2059 Pin
_Flaviu22-Aug-19 0:53
Member_Flaviu22-Aug-19 0:53 
AnswerRe: error C2059 Pin
Stefan_Lang21-Aug-19 4:08
mveStefan_Lang21-Aug-19 4:08 
QuestionC++ Program to decompress a compressed string Pin
antoniu20016-Aug-19 7:19
Memberantoniu20016-Aug-19 7:19 
AnswerRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 2:19
mveRichard MacCutchan17-Aug-19 2:19 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 2:39
Memberantoniu20017-Aug-19 2:39 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 4:02
mveRichard MacCutchan17-Aug-19 4:02 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 4:12
Memberantoniu20017-Aug-19 4:12 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 4:22
mveRichard MacCutchan17-Aug-19 4:22 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 4:24
Memberantoniu20017-Aug-19 4:24 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 4:59
mveRichard MacCutchan17-Aug-19 4:59 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 5:02
Memberantoniu20017-Aug-19 5:02 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 5:19
mveRichard MacCutchan17-Aug-19 5:19 
GeneralRe: C++ Program to decompress a compressed string Pin
David Crow19-Aug-19 5:49
MemberDavid Crow19-Aug-19 5:49 
AnswerRe: C++ Program to decompress a compressed string Pin
Patrice T17-Aug-19 20:16
mvePatrice T17-Aug-19 20:16 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20018-Aug-19 3:42
Memberantoniu20018-Aug-19 3: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.