Tech Off Thread

6 posts

template countof confusion-question

Back to Forum: Tech Off
  • Mr Crash

    These do the same thing (as far as i know) but which one is more correct ?

     

    Are there any pros and cons, etc ?

    Are there any differences at all that is noteworthy ?

     

    ---------------------------------------------------------------------------------------------------

    From: stdlib.h

     

     

    extern "C++" { template <typename _CountofType, size_t _SizeOfArray> char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; #define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0) }

     

     

    Question: Why "+ 0" ? To prevent compiler bug ?

    ---------------------------------------------------------------------------------------------------

    From: WinNT.h (Microsoft SDK)

     

     

    extern "C++" // templates cannot be declared to have 'C' linkage template <typename T, size_t N> char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N]; #define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))

     

     

    This is the same as the one above but without the strange "+ 0".

     

    ---------------------------------------------------------------------------------------------------

    From: WSPiApi.h (Microsoft SDK)

     

     

    template <typename __CountofType, size_t _N> char (&__wspiapi_countof_helper(__CountofType (&_Array)[_N]))[_N]; #define _WSPIAPI_COUNTOF(_Array) sizeof(__wspiapi_countof_helper(_Array))

     

    ---------------------------------------------------------------------------------------------------

     

    So the main question would be: pointer vs reference, pros and cons anyone know ?

     

  • Mr Crash

    Here's another one i found a few days ago,while searching for an answer:

     

    From: http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx

    I modified it to work in vs2010, i think i'm using decltype correctly here but i'm not sure ( decltype == typeof ? ) 

     

    template <class T> class ArraySize { }; template <class T, size_t N> class ArraySize<T[N]> { public: static const size_t value = N; }; //#define countofTEST(x) (ArraySize<typeof(x)>::value) // for GCC compiler #define countofTEST(x)
     (ArraySize<decltype(x)>::value) // works in vs2010 

     

     

  • Mr Crash

    Mr Crash said:

    Here's another one i found a few days ago,while searching for an answer:

     

    From: http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx

    I modified it to work in vs2010, i think i'm using decltype correctly here but i'm not sure ( decltype == typeof ? ) 

     

    template <class T> class ArraySize { }; template <class T, size_t N> class ArraySize<T[N]> { public: static const size_t value = N; }; //#define countofTEST(x) (ArraySize<typeof(x)>::value) // for GCC compiler #define countofTEST(x)
     (ArraySize<decltype(x)>::value) // works in vs2010 

     

     

    No one capable of answering these questions ?  Sad
    No c++/compiler experts around ? <shocked>

  • spivonious

    Mr Crash said:
    Mr Crash said:
    *snip*

    No one capable of answering these questions ?  Sad
    No c++/compiler experts around ? <shocked>

    They all do the same thing but with minor syntax differences (+0, pointer vs. reference). I'm not sure what you're looking for here. I'd expect their performance to be identical.

  • Mr Crash

    spivonious said:
    Mr Crash said:
    *snip*

    They all do the same thing but with minor syntax differences (+0, pointer vs. reference). I'm not sure what you're looking for here. I'd expect their performance to be identical.

    > I'm not sure what you're looking for here.

     

    Are anyone better over the others ? More c++ "correct"

     

    > They all do the same thing but with minor syntax difference

     

    yes but that minor syntax difference might make one of them better, more useful perhaps but since i'm not a c++ expert i ask in this forum thinking that there should be at least one in here that can answer this question.

     

    #define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0) 

    Why use the "+ 0", any special reason for it ?
    Is it a compiler hint of some sort ?

     

  • STL

    I made this change; I don't usually hack the CRT, but this one was trivial.  The + 0 silences a spurious "warning C6260: sizeof * sizeof is usually wrong. Did you intend to use a character count or a byte count?" from /analyze when someone writes _countof(arr) * sizeof(T).

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.