I'm using MSVS2013 and I rolled my own countof
macro
that accepts only TCHAR
arrays.
For reference, here is the code:
// Helper struct for _tcountof() macrotemplate<typename T,size_t N>struct _tcountof_struct_helper;// Helper partially specialized struct for _tcountof() macrotemplate<size_t N>struct _tcountof_struct_helper<TCHAR, N>{staticsize_tconst value = N;};// Helper function for _tcountof() macro. It's never called so no body is required.template<typename T,size_t N>auto _tcountof_function_helper(T(&)[N])->_tcountof_struct_helper<T, N>;// _tcountof(x) gets the size of a TCHAR array.// Will fail to compile if x is not of type TCHAR array.#define _tcountof(x)(decltype(_tcountof_function_helper(x))::value)
Now this works very well. It compiles and doesn't allow for any type other than a 1D array (unlike extent
that
MSVS implemented which allows for pointers as well as arrays, which of course should be invalid. It allows only TCHAR arrays and I may also modify it later to accept a number to say which dimension to query for an nD array) and acts like compile time constant.
However, the editor is being annoying in that red squiggly lines are appearing under the call to my _tcountof()
macro
and when I hover over it, it says:Error:
type name is not allowed
.
Is this just an instance of a failure of the editor not keeping up with the compiler's development? Is there a way to stop this red squiggly from appearing for this error or for a particular macro?