Skip to content

Instantly share code, notes, and snippets.

@DBJDBJ
Last active May 28, 2018 09:38
Show Gist options
  • Save DBJDBJ/67fb2e7c31d130acebb860728e11dc35 to your computer and use it in GitHub Desktop.
Save DBJDBJ/67fb2e7c31d130acebb860728e11dc35 to your computer and use it in GitHub Desktop.
Convincing MSVC that argv is an array (which it is not)
#include <iostreams>
// above should be enough
// dbjdbj 23 MAY 18 added
// this is VS2017 code
// linux twin is here
// https://wandbox.org/permlink/Wm3rtZhT3jCNfSXl
/// and behaves a bit better as in it manages to
// print the ARP value (see way bellow)
// print the type and value
// with optional prompt
template<typename T>
constexpr wchar_t tv_print (T && v_, const wchar_t prompt[] = L"" ) {
using v_type = std::remove_reference_t< T >;
std::wcout << prompt << L" { " << typeid(v_type).name() << L" : " << (v_) << " } " ;
return L' ';
};
#ifdef UNICODE
int wmain(const int argc, const wchar_t *argv[], const wchar_t *envp[])
#else
#error "What could be the [t]reason this is not UNICODE build?"
#endif
{
using namespace std;
// deliberately do not use template
auto print = [&](auto & x)
{
tv_print(x,L"\n\ninside auto print = [](auto & x)\nx : ");
using x_type = remove_reference_t< decltype(x) >;
static_assert(
is_array<x_type>() , "argument must be an native array reference"
);
size_t j{ 0 }; for (auto e : x)
std::wcout << L"\n [" << j++ << L"] : " << tv_print(e) ;
};
tv_print( argv[0], L"\n argv [0] : ");
// pointer to array type
typedef const char *(*ARP)[1];
// ref to array type
typedef const char *(&ARF)[1];
// HACK ALERT: above is effectively casting wchar_t into char
// this is WIN code so ... this might be a problem
// output is
// just the first letter of the
// full path
// on wandbox it is a the same as argv[0]
// https://wandbox.org/permlink/Wm3rtZhT3jCNfSXl
print( * (ARP)(argv));
// output is
// garbage
// seemingly on all platforms
// why?
print((ARF)(argv));
wcout.flush(); return EXIT_SUCCESS;
}
// EOF
@DBJDBJ
Copy link
Author

DBJDBJ commented May 28, 2018

HACK ALERT: above is effectively casting wchar_t into char, this is WIN code so ... this might be a problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment