4

I have this class which I want to pass around Windows as LPARAM parameter. Since it's small enough to fit inside LPARAM I want to pass it by value, but in case it expands later I want it to automatically switch to passing by reference.

I would use something like

typedef boost::call_traits<CMyClass>::param_type CMyClassParam;

which should be either const CMyClass or const CMyClass& depending on whether sizeof(CMyClass) <= sizeof(LPARAM) but call_traits optimizes only small PODs, not small classes.

typedef is taken care of by advice of Anycorn: typedef boost::mpl::if_c<(sizeof(CMyClass) <= sizeof(LPARAM)), CMyClass, CMyClass&>::type CMyClassParam;

And then, how do I convert between CMyClassParam and LPARAM (in case you don't know, LPARAM is int, long enough to store a pointer)

CMyClass::operator LPARAM()
{
    // must be either
    return *(LPARAM*)this;
    //or
    return reinterpret_cast<LPARAM>(this);
}
    //so that I may call:

void SomeWinFunc(LPARAM p);

CMyClass vi;
SomeWinFunc(vi);

// and then get it back:
void SomeWinCallback(LPARAM p)
{
    CMyClassParam vi = (?????)p;
    // which should translate into either
    CMyClass ti = *(CMyClass*)(&p); // make a bitwise copy
    // or
    CMyClass& ti = *(CMYClass*)p;
}
9
  • 1
    just write your own traits class. however, this appears to be a clear case of premature optimization. google what knuth said about that. Apr 9, 2012 at 8:16
  • In the most general usage of C++, you can't pack a struct into an int because the machine may have integral throwing NaNs that cause an exception just by copying the value. More practically, this will probably work but violates strict aliasing rules as written… unfortunately I don't know if I can help with those rules… Apr 9, 2012 at 8:36
  • It's a matter of passing local objects by value out of the function scope vs. creating them on the heap and implementing some garbage collector to keep track and dispose of them. This is the task that I'd like to optimize away (or defer until I absolutely have to).
    – panda-34
    Apr 9, 2012 at 8:59
  • @panda-34 passing the struct by value still degrades gracefully. Two machine words in registers/stack aren't catastrophically slower than one. Apr 9, 2012 at 9:05
  • @Potatoswatter Where do I put these two words? (dwords, actually) I'm given one int and that's all I'll ever get from windows. Whatever I have I must cram into it.
    – panda-34
    Apr 9, 2012 at 9:22

1 Answer 1

2

Something like that?

typedef typename mpl::if_c<(sizeof(T) <= MAX), T, T&>::type P;
P param = p;

but I'd think compiler would optimize this for you automagically

4
  • 1
    This metafunction has been standardized as std::conditional. Apr 9, 2012 at 8:21
  • 3
    Since C++0x became C++11 over a year ago, it has been the C++. Apr 9, 2012 at 8:23
  • @Potatoswatter : True, but then, how many compilers already implement that? And then, are you sure the user have access to that compiler? I'd be happy to have the two solutions, that is mpl::if_c and std::conditional, because then I have an answer no matter the generation of the compiler I'm using.
    – paercebal
    Apr 9, 2012 at 8:58
  • @paercebal Yes, this is the info I've provided… substitute std::conditional for mpl::if_c to translate it to C++11. Apr 9, 2012 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.