I have included file JSONValue from simpleJSON, which is used for parsing the json string.
While compiling I am getting this error that 'wcsncasecmp' was not declared in this scope.
on this line. While searching more i ot that wcsncasecmp is a GNU-specific function, I am using windows, so can anyone help me out.
else if ((simplejson_wcsnlen(*data, 4) && wcsncasecmp(*data, L"true", 4) == 0) || (simplejson_wcsnlen(*data, 5) && wcsncasecmp(*data, L"false", 5) == 0))
{
bool value = wcsncasecmp(*data, L"true", 4) == 0;
(*data) += value ? 4 : 5;
return new JSONValue(value);
}
.
I'll be greatful for any help.
On windows there is _wcsnicmp that you can use.
More ref: _strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
Please define WIN32 macro in your source or in Visual Studio please add it to the project / Properties / C/C++ / Preprocessor definition.
#define WIN32
Internally it will define wcsncasecmp as _wcsnicmp like Rohan has already mentioned.
This is a known problem of SimpleJSON.
// Win32 incompatibilities
#if defined(WIN32) && !defined(__GNUC__)
#define wcsncasecmp _wcsnicmp
static inline bool isnan(double x) { return x != x; }
static inline bool isinf(double x) { return !isnan(x) && isnan(x - x); }
#endif
Related
Everything is working as intended, and I get the values I need from va_arg(va_list, type), but I get this warning everywhere I call va_arg:
Warning C6285 (<non-zero constant> || <non-zero constant>) is always a non-zero constant. Did you intend to use the bitwise-and operator?
Example code:
void Logger::log(LogLevel level, const char* location, uint32_t line, const char* format, ...)
{
va_list arg_ptr;
va_start(arg_ptr, format);
while (*format) {
// ...
if (*format == 'd') { //
int i = va_arg(arg_ptr, int); // <-- Warning is reported here
// ...
}
// ...
++format;
}
// ...
va_end(arg_ptr);
}
Why do I get this warning and how can I get rid of it?
I'm using Visual Studio Community 2019 with Visual C++ 2019
C6### error codes are IntelliSense codes. These are based on heuristics and are meant to point the attention to potential errors, but can also result in false-positives, which seems to be the case here; it's probably triggering on the va_arg implementation in the CRT:
#define __crt_va_arg(ap, t) \
((sizeof(t) > sizeof(__int64) || (sizeof(t) & (sizeof(t) - 1)) != 0) \ // <== Here
? **(t**)((ap += sizeof(__int64)) - sizeof(__int64)) \
: *(t* )((ap += sizeof(__int64)) - sizeof(__int64)))
I would simply ignore it ...
If it bothers you, report it to the vendor: Help → Send Feedback → Report a Problem...
I'm trying to something like this:
if( constexpr( TEMPLATE_BOOL_VALUE ) || bOhterBoolValue )
{
Foo();
}
else
{
Baa();
}
I usually use a preprocessor macro for such an if statement:
#define IF_COND if( constexpr( TEMPLATE_BOOL_VALUE ) || bOhterBoolValue )
IF_COND
{
Foo();
}
else
{
Baa();
}
In Visual Studio 2017, this worked (I should have but didn't check the generated code, but the compiler accepted this syntax).
In Visual Studio 2019, now there is an error "C2760" because it only allows:
if constexpr( TEMPLATE_BOOL_VALUE )
{
Foo();
}
else if( bOhterBoolValue )
{
Foo();
}
else
{
Baa();
}
Is there another way for doing this without typing two times Foo()?
Hope you can help. I don't wanna get two identical cases.
Since Foo(); branch is valid (i.e. compiles) regardless of whether TEMPLATE_BOOL_VALUE is true or not, there no need to use if constexpr.
Simply use if (TEMPLATE_BOOL_VALUE || bOhterBoolValue).
I have this code inside a function but I am not able to understand what it does.
....
#define ascend(i) do {\
int h = nodes[i].heavyindex;\
int p = nodes[i].heavypos;\
m##i = max(m##i + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));\
i = paths[h].parent;\
} while (0)
while (nodes[a].heavyindex != nodes[b].heavyindex) {
if (nodes[a].heavyindex > nodes[b].heavyindex) {
ascend(a);
} else {
ascend(b);
}
}
#undef ascend
...
The code of #define, I think, is:
#define ascend(i) do {\
int h = nodes[i].heavyindex;\
int p = nodes[i].heavypos;\
m##i = max(m##i + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));\
i = paths[h].parent;\
} while (0)
so the real code inside the function is only this:
while (nodes[a].heavyindex != nodes[b].heavyindex) {
if (nodes[a].heavyindex > nodes[b].heavyindex) {
ascend(a);
} else {
ascend(b);
}
}
1) It is right?
2) I want to move the code of the #define inside a function to better understand what it does, but how I translate the following line?
m##i = max(m##i + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));\
Yes.
As mentioned by Ben Voigt in the comments, ## is the token-pasting operator. So with #define f(i) m##i defined, f(a) will expand to ma, f(b) will expand to mb, etc.
Since that's only possible with the preprocessor, you have to think of something else to implement it as a function. Passing ma and mb by reference would be a good idea. It could look something like this:
ascend(T& mi) {
...
mi = max(mi + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));
...
}
Where T is the type of ma and mb. If they're of different types, you need to make it a function template.
I'm trying to implement in log2 for integer in C++ in NaCl, I used the asm way as the nacl documentation said it's the only permitted way to write ASM, which is as follow
int log2(int x) {
int ret;
asm ( "\tbsr %1, %0\n"
: "=r"(ret)
: "r" (x)
);
return y;
}
, but turns out ARM does not support this instruction, so I want to write another version for ARM only. Is there any way to do that?
Btw, I found one solution to this particular function already, which is by using
static inline int log2(int x) {
return sizeof(int) * 8 - __builtin_clz(x) - 1;
}
mentioned in another post, so my question is purely about the way to give different implementation for different CPU Architecture. ( I've tried #ifdef ARCH_ARM, but it didn't work)
chromium native client use NACL_BUILD_ARCH to discriminate between x86, arm and mips : https://chromium.googlesource.com/chromium/chromium/+/trunk/components/nacl/nacl_defines.gypi
(NB: you can't use it if you're using PNaCl)
ex: (from here )
#elif NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 64
if (regs->prog_ctr >= NaClUserToSys(nap, NACL_TRAMPOLINE_START) &&
regs->prog_ctr < NaClUserToSys(nap, NACL_TRAMPOLINE_END)) {
*unwind_case = NACL_UNWIND_in_trampoline;
regs->stack_ptr += 8; /* Pop user return address */
return 1;
}
#elif NACL_ARCH(NACL_BUILD_ARCH) == NACL_arm
if (regs->prog_ctr >= NACL_TRAMPOLINE_START &&
regs->prog_ctr < NACL_TRAMPOLINE_END) {
*unwind_case = NACL_UNWIND_in_trampoline;
regs->prog_ctr = NaClSandboxCodeAddr(nap, regs->lr);
return 1;
}
#elif NACL_ARCH(NACL_BUILD_ARCH) == NACL_mips
if (regs->prog_ctr >= NACL_TRAMPOLINE_START &&
regs->prog_ctr < NACL_TRAMPOLINE_END) {
*unwind_case = NACL_UNWIND_in_trampoline;
regs->prog_ctr = NaClSandboxCodeAddr(nap, regs->return_addr);
return 1;
}
#endif
Also, bsr has an equivalent in arm if I recall correctly : http://fgiesen.wordpress.com/2013/10/18/bit-scanning-equivalencies/
In C++, is it possible to make a multi-statement macro with nested if statements inside of it like the one below? I've been attempting it for a while now and I'm getting a scope issue for the second if statement not being able to see 'symbol'. Maybe I need to understand macros further.
#define MATCH_SYMBOL( symbol, token)
if(something == symbol){
if( symbol == '-'){
}else if (symbol != '-'){
}
other steps;
}
For a multi-line macro you need to add a \ character to the end of all but the last line to tell the macro processor to continue parsing the macro on the next line, like so:
#define MATCH_SYMBOL( symbol, token) \
if(something == symbol){ \
if( symbol == '-'){ \
}else if (symbol != '-'){ \
} \
other steps; \
}
Right now, it's trying to interpret it as a 1-line macro and then some actual code at the top of your file, which isn't what you want:
#define MATCH_SYMBOL( symbol, token)
// and then... wrongly thinking this is separate...
if(something == symbol){ // symbol was never defined, because the macro was never used here!
if( symbol == '-'){
}else if (symbol != '-'){
}
other steps;
}
If you're using C++ you should avoid using macros altogether. They are not type-safe, they're not namespace-aware, they're hard to debug and just they're plain messy.
If you need a type-independent function, use templates:
template <typename T>
bool match_symbol(T symbol, T token) {
if(something == symbol){
if( symbol == '-'){
}else if (symbol != '-'){
}
...
or if the parameters can be different types:
template <typename T, typename V>
bool match_symbol(T symbol, V token) {
if(something == symbol){
if( symbol == '-'){
}else if (symbol != '-'){
}
...
Note that some of the answers here have a problem.
For example, for a normal statement you can do this:
if (foo)
function();
else
otherstuff();
If you followed some of the suggestions here, but if replace function with a macro, it might expand to:
if (foo)
if (something) { /* ... */ }
else { /* ... */ }; // <-- note evil semicolon!
else
otherstuff();
So a common (ugly) hack that people do to avoid this is:
#define MATCH_SYMBOL(symbol, token) \
do \
{ \
if(something == symbol) \
{ \
if( symbol == '-') \
{ \
} \
else if (symbol != '-') \
{ \
} \
other steps; \
} \
} \
while (0) // no semicolon here
This is so that the "statement" MATCH_SYMBOL(a, b) can end with a semicolon just like a normal statement. You also have braces around the multiple statements.
If you think nobody's crazy enough to use this technique, think again. It's very common in the Linux kernel, for example.
You need to have a backslash (\) at the end of all the lines in the macro but the last one.
The way of the C++:
inline void MATCH_SYMBOL(const Symbol& symbol, const Token& token) {
/* ... */
if (something == symbol) {
if ('-' == symbol) {
/* ... */
}
else if ('-' != symbol) {
/* ... */
}
}
/* ...other steps... */
}
also, see if you can replace the macro with a function.
?
MATCH_SYMBOL(Sym const & symbol, Tok const & token)
{
...
}
One can also define macro function and implement the function than
#define MATCH_SYMBOL( symbol, token) match_symbol(symbol,token)